Linking to a specific version of a shared library

I know how to link Rust code to a C shared library. But what if I want to link to a specific version? For example, my system contains both /lib/ and /lib/ . If my build script prints "cargo:rustc-link-lib=nv", then rust links to . How can I force it to link to instead?

You probably have a symlink from to The linker knows nothing about versions. You can either change the symlink (for example by installing the dev package for rather than or make a new symlink from say to in a different location and add this location to the linker search path and then use cargo:rustc-link-lib=foo. Whichever option you choose the linker will see a file which has as SONAME rather than and in response store in the executable rather than

Yes, I do have an symlink. But while manually changing it may work for me, it isn't automatable. It won't work for other users. Is there anything I can do that would be fully contained within the file?

The other option I gave should work without root permission. The new symlink can be in the OUT_DIR of the build script.

Thanks, that did the trick. FTR, this is the final code:

let out_dir = env::var("OUT_DIR").unwrap();
let link = Path::join(Path::new(&out_dir), "");
match fs::read_link(&link) {
    Ok(l) if l == link => (),
    Ok(_) => {
        unix::fs::symlink("/lib/", &link).unwrap();
    Err(_) => {
        unix::fs::symlink("/lib/", &link).unwrap();

// Link to libnv

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.