【问题标题】:What are the different ways of specifying the linking path to FFI libraries in Rust?在 Rust 中指定 FFI 库的链接路径有哪些不同的方法?
【发布时间】:2018-11-10 03:47:45
【问题描述】:

以下面的代码为例:

extern crate libc;

#[link(name = "adder")]
extern {
    fn double_input(input: libc::c_int) -> libc::c_int;
}

fn main() {
    let input = 4;
    let output = unsafe { double_input(input) };
    println!("{} * 2 = {}", input, output);
}

#[link(name = "adder")] 是否应该包含 .o / a / .h 文件的相对路径?例如,应该是#[link(name = "../adderlib/adder")]?还有其他方法可以告诉编译器adder 在哪里吗?

【问题讨论】:

    标签: rust extern ffi


    【解决方案1】:

    第一个问题的答案是肯定的!如果您的 lib 文件是 libfoo.o,那么您的代码中的 #[link(name = "foo") 就足够了。 official documentation有更多详情。

    它将相对于位于当前工作路径和系统lib路径中的lib文件。 (我在任何文档中都找不到这个,但我曾经成功地做到过)。您可以使用rustc -l XX -L XX 指定路径。使用带有 build script 的 Cargo 是一种更好的方法。

    【讨论】:

      【解决方案2】:

      如果您需要控制如何找到或链接到您的 Rust 代码的库,您应该通过build script 来实现。

      【讨论】:

        猜你喜欢
        • 2017-04-11
        • 1970-01-01
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多