简体   繁体   中英

What are the different ways of specifying the linking path to FFI libraries in Rust?

Using the below code as an example:

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);
}

Should #[link(name = "adder")] include a relative path to the .o / a / .h files? For example, should it be #[link(name = "../adderlib/adder")] ? Is there another way to tell the compiler where adder is?

The answer to the first question is YES! If your lib file is libfoo.o , #[link(name = "foo") is enough in your code. There are more details in the official documentation .

It will be relative to the lib file which is located in the current work path and the system lib path. (I cannot find this in any documentation, but I once made it successfully). You can specify a path using rustc -l XX -L XX . Using Cargo with a build script is a better way.

如果您需要控制如何查找库或将其链接到Rust代码,则应通过构建脚本进行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM