Cargo panics with FromBytesWithNulError when using bindgen with C++

139 Views Asked by At

Solved! People seem to agree on it being a bug that's fixed, just not in the current version.


Am currently learning Rust, cause it's the hot shit and will dethrone C++, bluh, bluh, and came across bindgen, which seems obviously important when interfacing with C/C++ APIs. Doing pretty much what the Rust Doc says when writing build.rs to invoke bindgen and it does work with C headers. But when I try to use C++ headers, about which the Doc just tells me to use a .hpp header or add clang arguments to force C++, it panics with FromBytesWithNulError somewhere in the code generation.

fn main() {
    println!("cargo:rerun-if-changed=wrapper.hpp");

    let bindings = bindgen::Builder::default().header("src/wrapper.hpp").generate()
        .expect("Can't generate bindings");

    let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
    bindings.write_to_file(out_path.join("bindings.rs"))
        .expect("Can't write bindings to file");
}

stderr is

--- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromBytesWithNulError { kind: InteriorNul(1) }', C:\Users\tidem\.cargo\registry\src\github.com-1ecc6299db9ec823\bindgen-0.66.1\codegen\mod.rs:717:71
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\std\src/panicking.rs:579:5
     1: core::panicking::panic_fmt
               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src/panicking.rs:64:14
     2: core::result::unwrap_failed
               at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src/result.rs:1750:5
     3: core::result::Result<T,E>::unwrap
     4: <bindgen::ir::var::Var as bindgen::codegen::CodeGenerator>::codegen
     5: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
     6: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen::{{closure}}
     7: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen
     8: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
     9: bindgen::codegen::codegen::{{closure}}
    10: bindgen::ir::context::BindgenContext::gen
    11: bindgen::codegen::codegen
    12: bindgen::Bindings::generate
    13: bindgen::Builder::generate
    14: build_script_build::main
    15: core::ops::function::FnOnce::call_once

Seems like it doesn't even reach my expect so no idea where to even start. Could be an obvious error, but literally started learning the language a few hours ago, so no clue.

I saw a GitHub Issue on the matter, but it was resolved and I'm on the newest version.

I tried telling cargo both wrapper.hpp and src/wrapper.hpp, neither of which worked, with the former (i think) working in C.

Also tried telling clang to force C++ with a .h file with seemingly no success.

Debug breaking build.rs doesn't seem to be a thing either, but the Stacktrace tells me it's a problem that's not for me to expect so whatever, but if someone wants to tell me how, that'd be great.

0

There are 0 best solutions below