This is a small repro of some linker errors we're seeing with rustc, Cargo, and
the cc
library.
The root of the problem is how rustc deduplicates redundant -l
flags when
calling the linker. This causes problems if we try to:
- link with multiple internal libraries ("A" and "B"), built using
cc
, - each of which depends on an external library (in this case, libstdc++),
- but which depend on different sets of symbols from that library.
If one of the libraries depends on a strict subset of the symbols needed by the other, and the "smaller" library is linked after the "larger" one, then the compilation happens to succeed. If you link the "smaller" library before the "larger" one, then the compilation fails. (But only on Linux.)
Verified on Linux:
git clone https://github.com/dcreager/rust-cc-linking
cd rust-cc-linking
cargo build -p cc-01-works-by-accident
cargo build -p cc-02-broken