[英]How do I make rustc-link-search relative to the project location?
I am creating a Rust wrapper around a C library.我正在围绕 C 库创建 Rust 包装器。 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:我已将 C 库放在
lib
目录中,并使用build.rs
告诉编译器在哪里可以找到要链接的库:
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:编译客户端库时,这并不指向
repository\\checked_out_project\\lib
而是似乎在本地查找,因为在依赖项中指定绝对路径有效:
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:我还指示 Cargo 在 wrapper-lib 中包含
lib
目录,如下所示:
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\");
Manually.手动。
A good example of this is the winapi
crate. winapi
crate 就是一个很好的例子。 It has a pair of sub-crates for import libraries, each of which has a build script and a lib
directory.它有一对用于导入库的子板条箱,每个子板条箱都有一个构建脚本和一个
lib
目录。 The build script for the i686
crate contains the following:i686
板条箱的构建脚本包含以下内容:
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());
}
The following should work for you:以下应该对您有用:
println!("cargo:rustc-link-search=native=./lib");
It`s going to be relative path, without any env variables.它将是相对路径,没有任何环境变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.