Linking against static Rust library: missing symbols from external crate

When attempting to linking against a statically compiled Rust library some symbols cannot be found. The static library depends on another crate.
Some of the undefined symbols relate to math functions:

gcc main.c -o main -Ltarget/debug -lrust
/usr/bin/ld: target/debug/librust.a(num_bigint-4e3863677219775c.num_bigint.57a2dbfad47afac9-cgu.09.rcgu.o): in function `std::f64::<impl f64>::exp':
/rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/std/src/f64.rs:416:(.text._ZN3std3f6421_$LT$impl$u20$f64$GT$3exp17h344c4bf96f1ecb40E+0xd): undefined reference to `exp'
/usr/bin/ld: target/debug/librust.a(num_bigint-4e3863677219775c.num_bigint.57a2dbfad47afac9-cgu.09.rcgu.o): in function `std::f64::<impl f64>::cbrt':
/rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/std/src/f64.rs:583:(.text._ZN3std3f6421_$LT$impl$u20$f64$GT$4cbrt17h72e20ba24def4bebE+0x8): undefined reference to `cbrt'
/usr/bin/ld: target/debug/librust.a(num_bigint-4e3863677219775c.num_bigint.57a2dbfad47afac9-cgu.09.rcgu.o): in function `std::f64::<impl f64>::ceil':
/rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/std/src/f64.rs:68:(.text._ZN3std3f6421_$LT$impl$u20$f64$GT$4ceil17h86b5f8d27115f649E+0xd): undefined reference to `ceil'
/usr/bin/ld: target/debug/librust.a(num_bigint-4e3863677219775c.num_bigint.57a2dbfad47afac9-cgu.09.rcgu.o): in function `std::f64::<impl f64>::log2::{{closure}}':
/rustc/5680fa18feaa87f3ff04063800aec256c3d4b4be/library/std/src/f64.rs:503:(.text._ZN3std3f6421_$LT$impl$u20$f64$GT$4log228_$u7b$$u7b$closure$u7d$$u7d$17h7977d169bdd29bc0E+0x11): undefined reference to `log2'
...

Another thread with a similar issue suggests including a system library.
How can one know which system libraries are necessary?

Add --print native-static-libs to the rustc commandline when compiling the staticlib. Eg using cargo rustc -- --print native-static-libs. This will make rustc emit a warning listing all necessary linker flags.

Using --print native-static-libs helps in finding all the dependencies. Coincidentially, I just found the answer in yet another thread :slight_smile:

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.