使用Cargo,如何将系统库添加到link命令?

时间:2018-10-19 19:13:36

标签: rust ffi rust-cargo

我试图在系统上调用标准C库(libstatgrab),但无法弄清楚如何使该库包含在链接行中。

我让Cargo运行wp_insert_post,其输出为:

build.rs

由于我的示例是一个小型原型,因此这里涉及的所有文件:

cargo:rustc-link-lib=statgrab cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu/

src/systemstats.rs

extern crate libc; #[repr(C)] #[derive(Clone, Copy, Debug)] struct sg_load_stats { min1: libc::c_double, min5: libc::c_double, min15: libc::c_double, systime: libc::time_t, } extern "C" { fn sg_get_load_stats() -> *mut sg_load_stats; } fn main() { unsafe { let load_stats = sg_get_load_stats(); println!("[load_stats] {:?}", *load_stats); } }

Cargo.toml

[package] name = "palimpsest" version = "0.1.0" authors = ["Savanni D'Gerinel <savanni@luminescent-dreams.com>"] build = "build.rs" [dependencies] libc = "0.2.43" [[bin]] name = "systemstats" path = "src/systemstats.rs"

build.rs

最后,生成输出:

fn main() {
    println!("cargo:rustc-link-lib=statgrab");
    println!("cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu/");
}

构建输出很难读,但是在其中看到root@0a2938cbf3b7:/src/palimpsest# cargo build Compiling palimpsest v0.1.0 (file:///src/palimpsest) Compiling libc v0.2.43 ls -l error: linking with `cc` failed: exit code: 1 | = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.1y16o1qfye96o7m0.rcgu.o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.3ewjf7fna93e7dd4.rcgu.o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.3rngp6bm2u2q5z0y.rcgu.o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.4oc10dk278mpk1vy.rcgu.o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.4xq48u46a1pwiqn7.rcgu.o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.51s1w397y42gpez1.rcgu.o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.8xzrsc1ux72v29j.rcgu.o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.oa3rad818d8sgn4.rcgu.o" "-o" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e" "/src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.crate.allocator.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/src/palimpsest/target/debug/deps" "-L" "/usr/lib/x86_64-linux-gnu/" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/src/palimpsest/target/debug/deps/liblibc-fe86c4c07bd339cb.rlib" "-Wl,--start-group" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-448e7f800092c158.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-187d239173779b21.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-1016cb92bf3e0c0f.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-404ca41e10f51fe1.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_system-3c640c2d04fa8073.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-6ea835c502ac484a.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-b77d79244be470bc.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-a51c1c47035430ae.rlib" "-Wl,--end-group" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-0413689b51de5d7d.rlib" "-Wl,-Bdynamic" "-lutil" "-lutil" "-ldl" "-lrt" "-lpthread" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil" = note: /src/palimpsest/target/debug/deps/systemstats-a71368fbbfd6ba9e.3ewjf7fna93e7dd4.rcgu.o: In function `systemstats::main': /src/palimpsest/src/systemstats.rs:19: undefined reference to `sg_get_load_stats' collect2: error: ld returned 1 exit status error: aborting due to previous error 的地方看不到"-L" "/usr/lib/x86_64-linux-gnu/"。我对文档的理解是-lstatgrab应该添加了该标志,但是没有。

令人沮丧的是,我已经完成了一些在MacOS上链接到Cocoa的工作,而我唯一需要的是在我的资料来源中一行:cargo:rustc-link-lib=statgrab,所以我不明白为什么可以这样做”只需使用类似的行#[link(name = "Cocoa", kind = "framework")]

1 个答案:

答案 0 :(得分:0)

结果我根本不需要build.rs。如果将库安装在非标准位置,或者我会这样做。但是,我现在可以成功链接并运行该应用程序。

首先,我从Cargo.toml中删除了build = "build.rs"

第二,我回到我的代码中,并将#[link(name = "statgrab", kind = "dylib")]移动到extern "C"节之前。

systemstats.rs

extern crate libc;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct sg_load_stats {
    min1: libc::c_double,
    min5: libc::c_double,
    min15: libc::c_double,
    systime: libc::time_t,
}

#[link(name = "statgrab", kind = "dylib")]
extern "C" {
    fn sg_init(ignore_errors: libc::c_int) -> libc::c_int;
    fn sg_get_load_stats() -> *mut sg_load_stats;
}


fn main() {
    unsafe {
        sg_init(1);
        let load_stats = sg_get_load_stats();
        println!("[load_stats] {:?}", *load_stats);
    }
}

然后输出:

root@040ee947460c:/src/palimpsest# cargo run
   Compiling palimpsest v0.1.0 (file:///src/palimpsest)
    Finished dev [unoptimized + debuginfo] target(s) in 1.54s
     Running `target/debug/systemstats`
[load_stats] sg_load_stats { min1: 0.09, min5: 0.05, min15: 0.01, systime: 1540037934 }
root@040ee947460c:/src/palimpsest#