Using dynamic C library without PATH modification

Hello, I'm linking against a dynamic C library on Windows, the Rust lib is then compiled as static lib and used in a C++ project via C FFI. Without modifying the PATH env variable it cannot find the C dynamic library because it is in another Path provided by Conan. Under Linux I would solve this via rpath, but as far as I know there is no equivalent on Windows. I want a solution where a user can still call cargo test on the Rust lib project and it just works without having them set environment variables. How could I achieve this?

The way Windows typically solves this is by making the installer place all required DLLs next to the EXE, and you can be guaranteed they'll always be loaded correctly because Windows always checks in the current directory before searching %PATH%. That's why you might go to C:\Program Files\Foo\ and see it have dozens of DLLs next to the main foo.exe.

For things like cargo test, you should be able to emit the cargo:ruistc-link-search=... message from your build script and cargo will make sure the folder is added to the search path. To quote the section from The Cargo Book I linked to...

These paths are also added to the dynamic library search path environment variable if they are within the OUT_DIR . Depending on this behavior is discouraged since this makes it difficult to use the resulting binary. In general, it is best to avoid creating dynamic libraries in a build script (using existing system libraries is fine).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.