Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking with a Windows library outside the build folder

Tags:

windows

rust

ffi

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

like image 852
Delta_Fore Avatar asked Jun 07 '15 15:06

Delta_Fore


People also ask

How do I create an object file and link to libraries?

( -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).

How do you link files together?

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.

How do I link to a library in Linux?

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.

How do I link libraries outside of $G?

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:


1 Answers

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.

like image 86
Vaelden Avatar answered Oct 05 '22 16:10

Vaelden