I have a crate, which needs dynamic libraries to be properly used.
The crate also provides a way to write tests and that's where the problems come in, since cargo test execution requires those dynamic libraries be specified inside PATH, which I don't want users to manually do, since for release and debug builds different dynamic libraries need to be used.
I solved this for the crate by reading CARGO_MANIFEST_DIR in a build script and copying the dlls to target/{profile}/deps, but to solve this for dependants is problematic, since I don't know where their target directories are. Are there any solutions to this?
With the -L CLI argument you can tell Rustc where to look for external dependencies, like native dynamic libraries. Rustc reads extra CLI arguments from the RUSTFLAGS environment variable. You can also configure Cargo to pass extra arguments to Rustc via [build.rustflags].
Why not? cargo::rustc-link-search=native=/path/to/native/lib/ should allow the linker to find shared libraries in the /path/to/native/lib/ directory.
Edit: ah wait, we are talking about dynamic libraries. You are right, paths passed via -L that are outside of OUT_DIR[1] aren't used by Cargo to look up dynamic libraries. From the docs, my emphasis:
Search paths included from any build script with the rustc-link-search instruction. Paths outside of the target directory are removed. It is the responsibility of the user running Cargo to properly set the environment if additional libraries on the system are needed in the search path.
Edit 2: Found a feature request for the ability to add search paths for dynamic libraries from a build script:
Edit 3: I confused OUT_DIR with [build.target-dir] here. OUT_DIR is a subdirectory inside the target directory and thus more restrictive than what Cargo actually adds to the search path for dynamic libraries. ↩︎