some months ago I decided to create some bindings for a C library that was not available in Rust. I first obtained a static library, and then linked to that in my build.rs file.
Since I would like to keep the original library updated with the latest changes, I decided to add the original project as a git dependency, and then build the library from Rust. My build.rs file now looks like this:
// use make to build the library
Command::new("make")
.arg("-C")
.arg("abPOA")
.output()
.expect("failed to invoke make");
// include the obtained library(-L ./lib -labpoa)
println!("cargo:rustc-link-search=abPOA/lib");
println!("cargo:rustc-link-lib=abpoa");
// bindgen stuff
This seems to work fine when running cargo build, however cargo test returns this error:
= note: /usr/bin/ld: cannot find -labpoa
collect2: error: ld returned 1 exit status
The problem is likely the relative path. Cargo has a different working directory when testing since integration tests are compiled as separate crates. Try to emit a link annotation containing an absolute path. You will probably want to use CARGO_MANIFEST_PATH or something similar as a reliable reference point.
Thanks for the reply, I changed my build.rs file to this:
let abpoa_dir = format!("{}/{}",env!("CARGO_MANIFEST_DIR"), "abPOA");
let abpoa_lib = format!("{}/{}",abpoa_dir, "lib");
// use make to build the library
Command::new("make")
.arg("-C")
.arg(abpoa_dir)
.output()
.expect("failed to invoke make");
// include abpoa (-L ./lib -labpoa)
println!("cargo:rustc-link-search={}", abpoa_lib);
println!("cargo:rustc-link-lib=abpoa");
However I'm getting the same error. It still works fine when building, but fails whenever I try to run some tests (or use this library as a dependency in another project).