How to put linker flags in the back of the cc command
Hello, Using the cc build command in build.rs like this:
cc::Build::new()
.compiler("g++-7")
.cpp(true)
.file("src/test.cpp")
.include("...")
.flag("-Llibrary_path")
.flag("-llibrary")
.compile("...");
this translates to roughly:
g++-7 -fPIC -I ... -Llibrary_path -llibrary src/test.cpp
Which errors with an undefined reference error, the same one that I have when compiling it via cargo.
It does compile when I but the -llibrary flag in the back:
g++-7 -fPIC -I ... -Llibrary_path src/test.cpp -llibrary
A solution I found here: https://stackoverflow.com/questions/50642574/how-can-i-specify-linker-flags-arguments-in-a-build-script
My question: can this be done via the cc::build command? How can I put it in the back?
Fixed it by adding the g++ -z rescan-now flag
println!("cargo:rustc-link-arg=-z rescan-now");
You have to keep in mind that cc.compile() method simply compiles a static library. Passing -l options has no effect in the context. This compiled library is linked without you doing anything special. If you want link with a pre-existing library, you should rather have your build script emit "cargo:rustc-link-lib" and "cargo:rustc-link-search" directives, see Build Scripts documentation.