I'm trying to include some C++ code in my crate. To do this I'm using the cc
crate to compile the code, and the bindgen
crate to generate bindings. I'm calling these two crates from my build.rs
file.
That step seems to work fine, the C++ code is compiled into an lib*.a
file, and the bindings are correctly generated.
However, when trying to actually get an executable out of it, the linking step fails.
Here is a very small reproducible test case: GitHub - LinusU/rust-cc-bindgen-example
When running cargo test
, the linking step fails with the following error:
Undefined symbols for architecture x86_64:
"Adder::setRight(int)", referenced from:
cc_bindgen_example::bindings::Adder::setRight::h023e63752d3e3b4e in cc_bindgen_example-e52b33219ce605dc.462vy2i9bt7whqws.rcgu.o
"Adder::setLeft(int)", referenced from:
cc_bindgen_example::bindings::Adder::setLeft::hebe26dbc9ad569e6 in cc_bindgen_example-e52b33219ce605dc.462vy2i9bt7whqws.rcgu.o
"Adder::Adder()", referenced from:
cc_bindgen_example::bindings::Adder::new::h46af834565d122ac in cc_bindgen_example-e52b33219ce605dc.462vy2i9bt7whqws.rcgu.o
"Adder::calculate()", referenced from:
cc_bindgen_example::bindings::Adder::calculate::ha39c14f4f1d75bfd in cc_bindgen_example-e52b33219ce605dc.462vy2i9bt7whqws.rcgu.o
"Adder::~Adder()", referenced from:
_$LT$cc_bindgen_example..Adder$u20$as$u20$core..ops..drop..Drop$GT$::drop::h67dac44c63546115 in cc_bindgen_example-e52b33219ce605dc.2b953osc9pvzliq.rcgu.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It also prints this notice:
note: ld: warning: ignoring file target/debug/build/cc-bindgen-example-a16d6bede43ed629/out/libadder.a, file was built for archive which is not the architecture being linked (x86_64): target/debug/build/cc-bindgen-example-a16d6bede43ed629/out/libadder.a
The strange thing is that if I inspect that libadder.a
file, it's indeed an x86_64 file:
$ lipo -info target/debug/build/cc-bindgen-example-a16d6bede43ed629/out/libadder.a
Non-fat file: target/debug/build/cc-bindgen-example-a16d6bede43ed629/out/libadder.a is architecture: x86_64
I've tried making sure that my toolchain is up to date, and tried running cargo clean
followed by a fresh cargo test
. I've also seen this in my actual package, and the test package I created to get a minimal test case.
Any help appreciated