Aleksander Fular
Aleksander Fular

Reputation: 873

How do I make rustc-link-search relative to the project location?

I am creating a Rust wrapper around a C library. I've put the C libraries in the lib directory and I am using build.rs to tell the compiler where to find the libraries to link with:

println!("cargo:rustc-link-lib=static=wrapped-lib");
println!(r"cargo:rustc-link-search=lib\");

This works fine when I build the library, but downstream libraries which depend on the wrapper library get compilation failures:

error: could not find native static library `wrapped-lib`, perhaps an -L flag is missing?

The problem seems to be with:

println!(r"cargo:rustc-link-search=lib\");

When compiling a client library, this does not point at repository\checked_out_project\lib but instead seems to be looking locally, because specifying the absolute path in the dependency works:

println!(r"cargo:rustc-link-search=C:\users\id\.cargo\..\lib\");

I also have instructed Cargo to include the lib directory in the wrapper-lib as follows:

include = ["lib/**/*"]

How do I tell the compiler to look relative to the dependency, not the project being built? I thought that this should work:

println!(r"cargo:rustc-link-search=lib\");

Upvotes: 9

Views: 14227

Answers (2)

Igor
Igor

Reputation: 373

The following should work for you:

println!("cargo:rustc-link-search=native=./lib");

It must be a relative path, without any env variables.

Upvotes: -1

DK.
DK.

Reputation: 58975

Manually.

A good example of this is the winapi crate. It has a pair of sub-crates for import libraries, each of which has a build script and a lib directory. The build script for the i686 crate contains the following:

use std::path::Path;
use std::env;

fn main() {
    let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    println!("cargo:rustc-link-search=native={}", Path::new(&dir).join("lib").display());
}

Upvotes: 22

Related Questions