Bindgen: Error generate bindings on files with included headers

Hi guys, I am working on the project that I need C++ bindings in Rust. I tried to use bindgen and it works perfectly with standalone file. However, with file that has included header files, it throws no file found error. For example, if file A.cpp includes B.cpp, when running bindgen, it throws B.cpp no file found. Are there any way to tell the bindgen build script where file is located?

Assuming that A.cpp and B.cpp are located in different directories and A.cpp does not give a full relative path to the latter, you can pass an -I argument to Clang with bindgen::Builder::clang_arg. For example:

let bindings = bindgen::builder()
    .header("A.cpp")
    .clang_arg("-Idir_containing_B_cpp")
    .generate()?;
1 Like

Why are you including implementation files in each other in the first place? You are only supposed to include header files. Source files included and then also separately compiled will likely cause other errors, such as violating the ODR. (And I'm pretty sure bindgen isn't expected to deal with such non-idiomatic setup in the first place.)

I was going to mention that, but I didn't want to make any assumptions. I just gave the benefit of the doubt that what OP called A.cpp and B.cpp are actually header files.

I agree that passing a source file to bindgen would be inadvisable, as well as including a source file in another source file (in general, at least). I'm not even sure if bindgen will detect a file with extension .cpp as being a C++ header.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.