我应该在哪里放置一个静态库,以便我可以将它与 Rust 程序链接? [英] Where should I place a static library so I can link it with a Rust program?

查看:32
本文介绍了我应该在哪里放置一个静态库,以便我可以将它与 Rust 程序链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将 C 库链接到 Rust.这是我所做的:

I do not know how to link a C library to Rust. Here's what I have done:

我的 lib.rs 文件包含

#[link(name = "test")]
extern {

该库已构建并具有名称 libtest.a.

The library is built and has the name libtest.a.

我不知道把它放在哪里.我已经尝试了几个地方,但是在执行 cargo run

I do not know where to put it. I have tried several places, but I still have errors of this type when doing 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 没有告诉我库应该在项目中的哪个位置.

I do not know where to put libtest.a so that /usr/bin/ld can find it. Cargo does not tell me where the library should be in the project.

我的Cargo.toml包含

[dependencies.test]
path = "./src/test"

[dependencies]
bitflags = "0.7"
libc = "0.2"

[build-dependencies]
make-cmd = "0.1"

再次阅读文档的 FFI 部分后,我想也许之前的报错信息是因为我在找共享库,所以做了如下修改:

After reading the FFI section of the documentation again, I thought that maybe the error messages from before were because I was looking for a shared library, so I made the following changes:

#[link(name = "test", kind = "static")]

经过这些更改后,我仍然不知道如何指示库的位置,但是现在消息告诉我:

After these changes, I still do not know how to indicate where the library is, but the message now tells me this:

error: could not find native static library `test`, perhaps an -L flag is missing?

推荐答案

我应该在哪里放置静态库

Where should I place a static library

随心所欲.您必须告诉编译器在哪里可以找到它.

Wherever you want. You have to tell the compiler where to find it.

首先,让我们创建一个静态库

First, let's create a static library

$ 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 的值指向我放置库的目录:

Next, we use a build script to set the value of rustc-link-search to point to the directory where I put the library:

fn main() {
    println!("cargo:rustc-link-search=/Projects/stack-overflow/using-c-static/");
}

我们现在可以使用库中的函数了:

We can now use the functions from the library:

#[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).我更喜欢这个,因为这样两个配置就彼此相邻.


That's the basic functionality. You could also use the build script to specify which library to link, instead of having it in your code (rustc-link-lib). I prefer this because then the two configurations are right next to each other.

您可能还应该遵循 *-sys 命名约定 并创建一个专门用于公开底层 API 的 crate.重要的是,这个 crate 应该指定 link 清单键 以避免在链接时重复符号.

You should also probably follow the *-sys naming convention and create a crate dedicated to exposing the underlying API. Importantly, this crate should specify the link manifest key to avoid duplicate symbols at linking time.

如果您的构建脚本需要更多信息,cargo 通过 环境变量.

If your build script needs more information, cargo passes many parameters via environment variables.

如果您将 C 代码编译为 crate 的一部分,您应该查看像 cccmake,这使得构建软件的过程变得更加容易.

If you are compiling C code as part of your crate, you should look into crates like cc or cmake, which make the act of building a piece of software much easier.

这篇关于我应该在哪里放置一个静态库,以便我可以将它与 Rust 程序链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆