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
( -L path to library *not including the library name* -l library name *excluding the prefix, lib, and suffix, .a *) The creation of an object file and linking must occur in two different steps. The first step includes the preprocessor replacing all header files and checking for syntax/function references (the -I flag).
They are simply linked together by listing the name of the files. How does this relate to libraries? Libraries are just a grouped together set of .o files. This means that when compiling they require inclusion of library header files when you create a .o file, as well as a linking stage when creating an executable.
An alternate way to link to the library is with the -L and -l commands. (Same as before.) ( -L path to library *not including the library name* -l library name *excluding the prefix, lib, and suffix, .a *) The creation of an object file and linking must occur in two different steps.
Linking with libraries outside of $G: Add directories to search for additional libraries to the "LIB_DIRS" line in the Makefile. For example to include the X11 libraries in your search path in Linux you could add:
I think the issue is that you are mistaking the manifest of your project with the cargo configuration.
Cargo.toml
file at the root of your project. It describes your project itself.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With