TLDR: Weird linking error that only shows up with cargo test
– not when using library as a dependency in a binary crate.
Longer: I have a *-sys crate that I’m using in a pure Rust crate to provide a safe FFI wrapper. The *-sys crate gets statically linked a lib_.a
file. I have some simple tests written in the library crate that I’d like to run. Linking fails when running cargo test
with this error:
... relocation R_X86_64_32S against '.data' can not be used when making a shared object; recompile with -fPIC
... error adding symbols: Bad value
collect2: error: ld returned 1 exit status
However, if I include the pure Rust wrapper crate as a dependency in a binary crate, everything builds OK. I can also run cargo test
from the binary crate without any linker errors.
At @mbrubeck’s suggestion on IRC, I looked into the linker args (cargo rustc -- -Z print-link-args
), and they appear to be functionally identical expect for the rlibs that are pulled in (and the original source file, of course).
Does anyone have any idea what I can do to get this linking correctly with cargo test? I’d eventually like to publish this to crates.io but I don’t want to do that if someone can’t clone it down and run the tests without embedding it in a binary crate.