如何在 Rust 中指定链接器路径? [英] How do I specify the linker path in Rust?

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

问题描述

我正在尝试将 Rust 程序与 libsoundio 链接起来.我使用的是 Windows,并且有 GCC 二进制下载可用.如果我把它和我的项目放在同一个文件夹中,我可以像这样链接它:

I'm trying to link a Rust program with libsoundio. I'm using Windows and there's a GCC binary download available. I can link it like this if I put it in the same folder as my project:

#[link(name = ":libsoundio-1.1.0/i686/libsoundio.a")]
#[link(name = "ole32")]
extern {
    fn soundio_version_string() -> *const c_char;
}

但我真的想指定 #[link(name = "libsoundio")] 甚至 #[link(name = "soundio")],然后在其他地方提供链接器路径.

But I really want to specify #[link(name = "libsoundio")] or even #[link(name = "soundio")], and then provide a linker path somewhere else.

在哪里可以指定该路径?

Where can I specify that path?

我尝试了 rustc-link-search 建议如下:

I tried the rustc-link-search suggestion as follows:

#[link(name = "libsoundio")]
#[link(name = "ole32")]
extern {
    fn soundio_version_string() -> *const c_char;
}

.cargo/config 中:

[target.i686-pc-windows-gnu.libsoundio]
rustc-link-search = ["libsoundio-1.1.0/i686"]
rustc-link-lib = ["libsoundio.a"]

[target.x86_64-pc-windows-gnu.libsoundio]
rustc-link-search = ["libsoundio-1.1.0/x86_64"]
rustc-link-lib = ["libsoundio.a"]

但它仍然只将 "-l" "libsoundio" 传递给 gcc 并以相同的 ld: cannot find -llibsoundio 失败.我错过了一些非常明显的东西吗?文档似乎表明这应该有效.

But it still only passes "-l" "libsoundio" to gcc and fails with the same ld: cannot find -llibsoundio. Am I missing something really obvious? The docs seem to suggest this should work.

推荐答案

构建脚本文档:

所有由构建脚本打印到标准输出的行 [...开始] 与 cargo: 直接由 Cargo [...] rustc-link-search 解释> 表示指定的值应该作为 -L 标志传递给编译器.

All the lines printed to stdout by a build script [... starting] with cargo: is interpreted directly by Cargo [...] rustc-link-search indicates the specified value should be passed to the compiler as a -L flag.

在您的 Cargo.toml 中:

[package]
name = "link-example"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]
build = "build.rs"

还有你的 build.rs:

fn main() {
    println!(r"cargo:rustc-link-search=C:Rustlinkalibsoundio-1.1.0i686");
}

请注意,您的构建脚本可以使用 Rust 的所有功能,并且可以根据目标平台(例如 32 位和 64 位)输出不同的值.

Note that your build script can use all the power of Rust and can output different values depending on target platform (e.g. 32- and 64-bit).

最后,您的代码:

extern crate libc;

use libc::c_char;
use std::ffi::CStr;

#[link(name = "soundio")]
extern {
    fn soundio_version_string() -> *const c_char;
}

fn main() {
    let v = unsafe { CStr::from_ptr(soundio_version_string()) };
    println!("{:?}", v);
}

证据就在布丁里:

$ cargo run
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `targetdebuglinka.exe`
"1.0.3"

<小时>

理想情况下,您将创建一个 soundio-sys 包,使用 *-sys 包的约定.这只是一个链接到适当库并公开 C 方法的构建脚本.它将使用 货物 links 以唯一标识本机库并防止多次链接到它.然后其他库可以包含这个新的 crate,而不必担心这些链接细节.


Ideally, you will create a soundio-sys package, using the convention for *-sys packages. That simply has a build script that links to the appropriate libraries and exposes the C methods. It will use the Cargo links key to uniquely identify the native library and prevent linking to it multiple times. Other libraries can then include this new crate and not worry about those linking details.

这篇关于如何在 Rust 中指定链接器路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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