简体   繁体   English

如何相对于项目位置进行 rustc-link-search?

[英]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.

相关问题 无法添加到依赖项的 rustc-link-search 路径 - Unable to add to the rustc-link-search path of a dependency 如何更改默认的 rustc / Cargo 链接器? - How do I change the default rustc / Cargo linker? 我如何使“ rustc”的注释静音,以免链接到哪些本地工件? - How can I silence `rustc`'s note about which native artifacts to link against? 如何配置SublimeLinter-contrib-rustc以找到“活塞”箱? - How do I configure SublimeLinter-contrib-rustc to find the “piston” crate? 如何指定交叉编译rustc时要使用的编译器? - How do I specify the compiler to use when cross-compiling rustc? 使用 rustc 编译代码时如何指定要使用的版本? - How do I specify the edition to use when compiling code using rustc? 如何在现有项目中对Cargo进行类似的依赖解析,并且只使用rustc进行代码分析? - How can I get similar dependency resolution to Cargo in an existing project with only rustc for code analysis? 如何仅使用rustc而不使用商品链接动态Rust库? - How to link a dynamic Rust library using only rustc and not cargo? 我如何查询主机三元组的 rustc? - How can I query rustc for the host triple? 我应该如何更新 Rustup 的版本? 不更新 rustc 或 cargo 的版本 - How should I update the version of Rustup? not updating the version of rustc or cargo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM