How to specify linker path in Rust?

I am trying to link a Rust program to libsoundio . I use Windows, and there is 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;
}

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

Where can I point this way?

I tried the sentence rustc-link-searchas follows:

#[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"]

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

+4
3

script:

, stdout script [... start] cargo:, Cargo [...] rustc-link-search, , -L.

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:\Rust\linka\libsoundio-1.1.0\i686");
}

, script 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. script, C. Cargo links key, . .

+7

-, : links Cargo.toml:

[package]
links = "libsoundio"
build = "build.rs"

, libsoundio. .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"]

( : GCC , lib - .)

build.rs:

fn main() {}

, .cargo/config , - Cargo - , links =, build =, .

, main.rs:

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

C rustcuse -Land -L:

$ rustc --help
...
-L [KIND=]PATH      Add a directory to the library search path. The
                    optional KIND can be one of dependency, crate, native,
                    framework or all (the default).
-l [KIND=]NAME      Link the generated crate(s) to the specified native
                    library NAME. The optional KIND can be one of static,
                    dylib, or framework. If omitted, dylib is assumed.
...

Please note: if -Lyou must discard the prefix liband extension of .ayour static library:-lsoundio

0
source

Source: https://habr.com/ru/post/1662083/


All Articles