[英]Where should I place a static library so I can link it with a Rust program?
我不知道如何将 C 库链接到 Rust。 这是我所做的:
我的lib.rs文件包含
#[link(name = "test")]
extern {
该库已构建并具有名称libtest.a
。
我不知道把它放在哪里。 我已经尝试了几个地方,但是在执行cargo run
时仍然出现这种类型的错误
error: linking with `cc` failed: exit code: 1
//..
note: /usr/bin/ld: no se puede encontrar -ltest
note: /usr/bin/ld: no se puede encontrar -ltest
note: /usr/bin/ld:.......
//..
上面的翻译/usr/bin/ld: no se puede encontrar -ltest
-> usr/bin/ld: cannot find -ltest
我不知道将libtest.a放在哪里,以便/usr/bin/ld
可以找到它。 Cargo 没有告诉我库应该在项目中的哪个位置。
我的Cargo.toml包含
[dependencies.test]
path = "./src/test"
[dependencies]
bitflags = "0.7"
libc = "0.2"
[build-dependencies]
make-cmd = "0.1"
再次阅读文档的FFI部分后,我认为可能之前的错误信息是因为我正在寻找共享库,因此我进行了以下更改:
#[link(name = "test", kind = "static")]
在这些更改之后,我仍然不知道如何指示库的位置,但是现在消息告诉我:
error: could not find native static library `test`, perhaps an -L flag is missing?
我应该在哪里放置静态库
哪里都行。 您必须告诉编译器在哪里可以找到它。
首先,让我们创建一个静态库
$ cat hello.c
int square(int value) {
return value * value;
}
$ gcc -c -o hello.o hello.c
$ ar rcs libhello.a hello.o
接下来,我们使用构建脚本将rustc-link-search
的值设置为指向我放置库的目录:
fn main() {
println!("cargo:rustc-link-search=/Projects/stack-overflow/using-c-static/");
}
我们现在可以使用库中的函数:
#[link(name = "hello")]
extern "C" {
fn square(val: i32) -> i32;
}
fn main() {
let r = unsafe { square(3) };
println!("3 squared is {}", r);
}
这就是基本功能。 您还可以使用构建脚本来指定要链接的库,而不是将其包含在您的代码中 ( rustc-link-lib
)。 我更喜欢这样,因为这两种配置彼此相邻。
您可能还应该遵循*-sys
命名约定并创建一个专门用于公开底层 API 的板条箱。 重要的是,此 crate 应指定link
清单键以避免link
出现重复符号。
如果您的构建脚本需要更多信息,cargo 通过环境变量传递许多参数。
如果您将 C 代码编译为 crate 的一部分,您应该查看 crate 之类的cc或cmake ,这使得构建软件的行为变得更加容易。
我刚刚找到了一个解决方案(二),但我不知道这是否是最好的方法:
1路
build.rs 文件
extern crate gcc;
fn main() {
println!("cargo:rustc-link-search=native=/home/path/to/rust/proyect/folder/contain/file.a");
println!("cargo:rustc-link-lib=static=test");
}
Cargo.toml
//..
build = "build.rs"
//..
2路
Cargo.toml
//..
rustc-link-search = ["./src/lib"]
rustc-link-lib = ["test"]
root = "/home/path/to/rust/proyect/"
//..
["./src/lib"]
-> 放置到 lib.a 对 root 的引用
如果你不想指定库的路径,而且你在 Linux 上,你可以把它放在“usr/local/lib”中,然后在 main.rs 中说出它的名字,如下所示:
#[link(name = "theLibraryname", kind = "")]
此外,您可以将任何头文件放在包含文件夹中。
注意:库名称没有lib
前缀和.a
扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.