Link failure when trying to use `cc` & `bindgen` to link C++ code

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 :heart:

https://asciinema.org/a/2mkbupiBvH21AWn9As7XXnM9f

Hard to help you to solve the error when I encounter none :sweat_smile:

Context:

$ uname - a
Linux ship 4.10.0-38-generic #42~16.04.1-Ubuntu SMP Tue Oct 10 16:32:20 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

$ cargo -V
cargo 1.33.0 (f099fe94b 2019-02-12)

$ rustc -V
rustc 1.33.0 (2aa4c46cf 2019-02-28)

Interesting! I should probably have mentioned my system in the initial post :see_no_evil:

macOS Mojave 10.14.4

$ uname -a
Darwin name 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64

$ cargo -V
cargo 1.35.0-nightly (6f3e9c367 2019-04-04)

$ rustc -V
rustc 1.35.0-nightly (96d700f1b 2019-04-10)

edit: Just tried with the stable release as well, same error:

$ rustup run stable cargo -V  
cargo 1.33.0 (f099fe94b 2019-02-12)
$ rustup run stable rustc -V
rustc 1.33.0 (2aa4c46cf 2019-02-28)

Here is the output of cargo clean && cargo build -vv. There may be an issue with your cc / c++ compiler / linker, or with ar

Finally figured out the root cause of this! The problem was that my ar and runlib was the gnu versions, while the rest of the toolchain was Xcode. The fix was to make sure that /usr/bin was first in the path.

Here is a Stack Overflow post describing this in detail: c++ - Static library built for archive which is not the architecture being linked (x86_64) - Stack Overflow

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.