Library search paths as an environment variable

I’m compiling a Rust program on Windows and I need to link to an external C library statically.

I know where the .lib file is on my computer and the compilation works if I provide the -L flag with an absolute path to the directory where the .lib file exists.

What I want to know is, can I just add this path to an environment variable instead of having to provide Rust with the -L flag every single time I compile?

I tried setting the LIB environment variable, which I think is the one MSVC uses, and I even tried setting the LIBRARY_PATH environment variable. But Rust doesn’t seem to pick up on these, and compilation fails unless I provide the path with the -L flag.

This is usually solved by making a sys crate that has a build.rs script that reads a predefined library-specific env var, like LIBRARYNAME_PATH, and tells Rust/Cargo to use it via printing cargo:rustc-link-search=native=.

1 Like

Thanks for the link, it really helps. I didn’t know you could set up builds like that at all :slight_smile: