我正在尝试将 Rust 程序与库声音库 http://libsound.io/。我使用的是 Windows,并且可以下载 GCC 二进制文件。如果我将它放在与我的项目相同的文件夹中,我可以像这样链接它:
#[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")]
,然后在其他地方提供链接器路径。
我在哪里可以指定该路径?
我尝试过rustc-link-search
建议如下:
#[link(name = "libsoundio")]
#[link(name = "ole32")]
extern {
fn soundio_version_string() -> *const c_char;
}
And in .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
。我是否遗漏了一些非常明显的东西?文档似乎表明这应该可行。
如中所述构建脚本的文档 http://doc.crates.io/build-script.html#outputs-of-the-build-script:
构建脚本打印到标准输出的所有行 [...开始]cargo:
由 Cargo 直接解释 [...]rustc-link-search
指示指定的值应作为 a 传递给编译器-L
flag.
In your Cargo.toml:
[package]
name = "link-example"
version = "0.1.0"
authors = ["An Devloper <[email protected] /cdn-cgi/l/email-protection>"]
build = "build.rs"
和你的build.rs:
fn main() {
println!(r"cargo:rustc-link-search=C:\Rust\linka\libsoundio-1.1.0\i686");
}
请注意,您的构建脚本可以使用 Rust 的所有功能,并且可以根据目标平台(例如 32 位和 64 位)输出不同的值。
最后,你的代码:
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
包,使用公约*-sys包 http://doc.crates.io/build-script.html#-sys-packages。它只是有一个构建脚本,链接到适当的库并公开 C 方法。它将使用Cargo links key http://doc.crates.io/build-script.html#the-links-manifest-key唯一标识本机库并防止多次链接到它。然后,其他库可以包含这个新包,而不必担心那些链接细节。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)