在Rust中指定到FFI库的链接路径有哪些不同的方法? [英] What are the different ways of specifying the linking path to FFI libraries in Rust?

查看:207
本文介绍了在Rust中指定到FFI库的链接路径有哪些不同的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下面的代码为例:

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在哪里?

解决方案

第一个问题的答案是肯定的!如果您的lib文件是libfoo.o,则在您的代码中#[link(name = "foo")就足够了. 官方文档.

>

它将相对于位于当前工作路径和系统lib路径中的lib文件. (我无法在任何文档中找到它,但是我曾经成功地做到了).您可以使用rustc -l XX -L XX指定路径.结合使用构建脚本的货运是一种更好的方法. /p>

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中指定到FFI库的链接路径有哪些不同的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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