Locating a locally built shared library dependency at runtime is not possible #9016
Labels
A-build-scripts
Area: build.rs scripts
A-linkage
Area: linker issues, dylib, cdylib, shared libraries, so
C-feature-request
Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
S-triage
Status: This issue is waiting on initial triage.
Describe the problem you are trying to solve
We are writing a
-sys
crate, which can either link to a globally installed library, or build it for you from source, using thecmake
crate.The latter makes dynamic linking essentially impossible, because there is no way to tell the consumer of the crate where to find the compiled library at runtime
The following problems arise:
rpath
of any executable or library linking to the built library, which Cargo has no way of specifying beyond settingRUSTFLAGS
, which can be considered at best a workaround. Furthermore, on Windows, which does not have any equivalent of this, the library would have to be either in the process' CWD, or in the system's PATH.$CARGO_TARGET_DIR/$PROFILE/build/$CRATE_NAME-$CRATE_HASH/out/lib
?) to where your built executable is ($CARGO_TARGET_DIR/$PROFILE
?). Manual copying of files between different crate's build directories should never be necessary for the user.Most of these variables are not actually available to a build script in the first place, and even if they were, it would require the executable to have its own build.rs, tailored specifically to the crate it is depending on.
cargo install
will blow the whole thing up, because it will only install the executable, but not any libraries it may depend on.This, in effect means that Rust and Cargo offer two ways to link to a native library:
Describe the solution you'd like
From looking at various other issues, it seems it would be nice if crates could specify that their build script will generate its own artifacts (whatever those may be), and pass the location of said artifacts to cargo and other build scripts using a
cargo:
print directive, and for crates depending on said artifacts to specify such (think of it like specifying crate features).This way, a
libexample-sys
crate could specify it creates an artifactabc
, which consumers of the crate could then specifically request for cargo to copy to where it is needed (next to built executables / examples / libraries). (although ideally, consumers should not have to worry at all about this. this issue might need a rewrite if i can formulate the problem better)Something along these lines (consider this nothing more than a very rough idea)
It seems that this RFC is what we would want.
The text was updated successfully, but these errors were encountered: