6
votes

Is there a way to link with a library that's not in the current package path.

This link suggests placing everything under the local directory. Our packages are installed in some repository elsewhere. I just want to specify the libpath to it on windows.

authors = ["Me"]
links = "CDbax"

[target.x86_64-pc-windows-gnu.CDbax]
rustc-link-lib = ["CDbax"]
rustc-link-search = ["Z:/Somepath//CPP/CDbax/x64/Debug/"]
root = "Z:/Somepath//CPP/CDbax/x64/Debug/"

But trying cargo build -v gives me

package `hello v0.1.0 (file:///H:/Users/Mushfaque.Cradle/Documents/Rustc/hello)` specifies that it links to `CDbax` but does not have a custom build script

From the cargo build script support guide, it seems to suggest that this should work. But I can see that it hasn't added the path. Moving the lib into the local bin\x68_64-pc-windows-gnu\ path works however.

Update Thanks to the answer below, I thought I'd update this to give the final results of what worked on my machine so others find it useful.

In the Cargo.toml add

links = "CDbax"
build = "build.rs"

Even though there is no build.rs file, it seems to require it (?) otherwise complains with

package `xxx v0.1.0` specifies that it links to `CDbax` but does not have a custom build script

Followed by Vaelden answer's create a 'config' file in .cargo

If this is a sub crate, you don't need to put the links= tag in the parent crate, even though it's a dll; even with a 'cargo run'. I assume it adds the dll path to the execution environment

1

1 Answers

6
votes

I think the issue is that you are mistaking the manifest of your project with the cargo configuration.

  • The manifest is the Cargo.toml file at the root of your project. It describes your project itself.
  • The cargo configuration describes particular settings for cargo, and allow for example to override dependencies, or in your case override build scripts. The cargo configuration files have a hierarchical structure:

Cargo allows to have local configuration for a particular project or global configuration (like git). Cargo also extends this ability to a hierarchical strategy. If, for example, cargo were invoked in /home/foo/bar/baz, then the following configuration files would be probed for:

/home/foo/bar/baz/.cargo/config
/home/foo/bar/.cargo/config
/home/foo/.cargo/config
/home/.cargo/config
/.cargo/config

With this structure you can specify local configuration per-project, and even possibly check it into version control. You can also specify personal default with a configuration file in your home directory.

So if you move the relevant part:

[target.x86_64-pc-windows-gnu.CDbax]
rustc-link-lib = ["CDbax"]
rustc-link-search = ["Z:/Somepath//CPP/CDbax/x64/Debug/"]
root = "Z:/Somepath//CPP/CDbax/x64/Debug/"

to any correct location for a cargo configuration file, it should work.