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

此生再无相见时 提交于 2019-12-11 12:37:43

问题


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?


回答1:


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.




回答2:


If you need to control how a library is found or linked to your Rust code, you should do so via a build script.



来源:https://stackoverflow.com/questions/50619070/what-are-the-different-ways-of-specifying-the-linking-path-to-ffi-libraries-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!