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

查看:752
本文介绍了如何在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-搜索建议如下:

#[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:找不到-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.

推荐答案

有关构建脚本的文档


由构建脚本打印到stdout的所有行[...从 cargo开始:由Cargo [...] <直接解释code> 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"

以及您的构建。 rs

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

请注意,您的构建脚本可以使用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 `target\debug\linka.exe`
"1.0.3"






理想情况下,您将使用soundio-sys 包。 script.html#-sys-packages rel = noreferrer> *-sys 软件包的约定。那只是有一个构建脚本,该脚本链接到适当的库并公开C方法。它将使用货物链接,以唯一标识本机库并防止多次链接。然后,其他库可以包含这个新的板条箱,而不必担心那些链接详细信息。


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天全站免登陆