【发布时间】:2014-12-02 12:28:58
【问题描述】:
有什么方法可以告诉 Rust 在哪里寻找我的静态库?示例代码
#[link(name = "/this/is/the/path/libfoo.a", kind = "static")]
如果没有,我可以进行哪些配置更改或将我的库放在哪个文件夹中以便我可以使用它?
【问题讨论】:
标签: rust
有什么方法可以告诉 Rust 在哪里寻找我的静态库?示例代码
#[link(name = "/this/is/the/path/libfoo.a", kind = "static")]
如果没有,我可以进行哪些配置更改或将我的库放在哪个文件夹中以便我可以使用它?
【问题讨论】:
标签: rust
rustc 调用系统链接器,该链接器在库目录中查找#[link(...)] 中指定的所有库。通常有几个默认库目录(如 Linux 上的 /lib 和 /usr/lib),并且可以通过链接器标志指定更多目录(rustc 接受 -L 选项,然后将其传递给链接器)。
如果您直接调用rustc,您可以使用-L 选项添加额外的库目录,然后将其传递给链接器。但是,如果您使用 Cargo,您还有更多选择:
Cargo 将/target/<profile>/deps 目录添加为库源目录。
你可以使用cargo rustc
cargo rustc -- -L /path/to/library/directory
您可以指定RUSTFLAGS 环境变量:
RUSTFLAGS='-L /path/to/library/directory' cargo build
您可以使用构建脚本来output more linker options
println!("cargo:rustc-link-lib=static=foo");
println!("cargo:rustc-link-search=native=/path/to/foo");
我认为,最简单的方法是添加a custom build script,它将复制或创建符号链接到相应/target/<profile>/deps 目录中的库。
【讨论】:
<project_root>/build、<project_root>/build/native/* 和 <project_root>/deps 之后,我仍然无法让它工作:( 错误:could not find native static library 'demo', perhaps an -L flag is missing?。我的库名称是libdemo.a
target/<whatever directory>,而不是build/。我不知道为什么我写了build/ :( 另外你不能把你的文件放在那里,这个目录在每次构建之前都会被删除。你需要构建脚本来为你做这件事。见here ,例如。
要补充已接受的答案,对我有用的方法如下:
target/debug/deps下工作;但是将文件放在target/debug/native/* 下似乎不起作用。默认情况下,货物似乎只在target/debug/deps 下查看。
您可以使用cargo build --verbose 运行以查看详细的rustc 命令和使用的选项。 -L 选项指定附加链接依赖目录。
【讨论】:
cargo build --verbose doesn't show the actual linker command,所以它的用处值得怀疑。无论如何,我已经更新了接受的答案,不再是不准确的。
cargo (v0.26.0),它确实表示rustc 模式下rustc 命令的-L 选项。 github issue 可能已经修复了。
rustc的命令,而不是到链接器的命令。