Setting rustc-env=PATH on windows to find DLLs

I have some crates that build and link to C++ dynamic libraries via a static library shim. If I want cargo run or cargo test to work I need to point the environment to those libraries. On linux I can just do:

println!("cargo:rustc-env=LD_LIBRARY_PATH={}", lib_path.display());

On Windows the equivalent variable is just PATH, so I tried:

println!("cargo:rustc-env=PATH={};{}", bin_path.display(), std::env::var("PATH").unwrap());

but this doesn't seem to work. I've checked the generated path and it's correctly pointing to my DLLs and indeed if I set that path manually in the shell, then do cargo test it works as expected, just not via cargo:rustc-env=PATH. Is there a way to make this work or am I going to have to copy DLLs around in the build script?

I don't think you should be fiddling with LD_LIBRARY_PATH or PATH here.

You can directly tell rustc which paths to search when trying to link to a library by printing cargo:rustc-link-search=path/to/lib/dir/ from your build script.

1 Like

Ah you're right, thanks. Key thing here was that DLLs were getting put in /bin, while my current link-search was pointing to /lib.

2 Likes

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.