如何告诉 Rust 去哪里寻找静态库? [英] How to I tell Rust where to look for a static library?
问题描述
有什么方法可以告诉 Rust 去哪里寻找我的静态库?示例代码
Is there any way to tell Rust where to look for my static library? Example code
#[link(name = "/this/is/the/path/libfoo.a", kind = "static")]
如果没有,我可以进行哪些配置更改或将我的库放在哪个文件夹中以便我可以使用它?
If not, what config change can I make or what folder do I place my library in so that I can use it?
推荐答案
rustc
调用系统链接器,它会查找 #[link(...)]
#[link(...)] 中指定的所有库代码> 在库目录中.通常有几个默认的库目录(例如 Linux 上的 /lib
和 /usr/lib
),并且可以通过链接器标志(rustc
> 接受 -L
选项,然后将其传递给链接器).
rustc
invokes the system linker which looks for all libraries specified in #[link(...)]
in library directories. There are usually several default library directories (like /lib
and /usr/lib
on Linux), and more can be specified via linker flags (rustc
accepts -L
options which it then passes through to the linker).
如果您直接调用 rustc
,您可以使用 -L
选项添加额外的库目录,然后将这些目录传递给链接器.但是,如果您使用 Cargo,您还有更多选择:
If you invoke rustc
directly, you can use the -L
option to add additional library directories which will be then passed through to the linker. If you use Cargo, however, you've got a few more options:
Cargo 添加
/target/
目录作为库源目录./deps
你可以使用cargo rustc
cargo rustc -- -L /path/to/library/directory
您可以指定 RUSTFLAGS
环境变量:
RUSTFLAGS='-L /path/to/library/directory' cargo build
您可以使用构建脚本来输出更多链接器选项
println!("cargo:rustc-link-lib=static=foo");
println!("cargo:rustc-link-search=native=/path/to/foo");
我认为最简单的方法是添加 一个自定义构建脚本,它将在相应的/target/
目录中复制或创建指向您的库的符号链接.
The easiest way for you, I think, is to add a custom build script which will copy or create a symlink to your library in the corresponding /target/<profile>/deps
directory.
这篇关于如何告诉 Rust 去哪里寻找静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!