簡體   English   中英

在Rust FFI中混合靜態和動態庫

[英]Mixing static and dynamic libraries in Rust FFI

我的可執行文件Rust crate使用本機庫libfoo.a ,它依賴於共享庫libbar.so ,但根本不暴露它。

我的Rust FFI使用libfoo中的方法,所以我在我的extern代碼上定義了一個link屬性::

#[link(name="foo", kind="static")]
extern "C"{
    pub fn do_foo();
}

和我的build.rs包含來自Cargo.toml使用build="build.rs"

fn main() {
    let libs = &[(Some("../libs/foo/1.0/"), "static=foo"), // use ../libs/foo/1.0/libfoo.a
                 (None, "bar")]; // use libbar.so using LD_LIBRARY_PATH
    for &(ref m_path, ref lib) in libs {
        if let &Some(static_path) = m_path {
            println!("cargo:rustc-link-search={}", &static_path);
        }
        println!("cargo:rustc-link-lib={}", &lib);
    }
}

哪個輸出

cargo:rustc-link-search=../libs/foo/1.0/
cargo:rustc-link-lib=static=foo
cargo:rustc-link-lib=bar

從理論上講,我希望Rust能夠鏈接到libfoo.alibbar.so 問題是rustc甚至沒有嘗試承認libbar

cargo build --debug結束

/home/author/src/foo/foo.c:21: undefined reference to 'bar_do_stuff'
collect2: error: ld returned 1 exit status

當我檢查鏈接器命令時,有一個-L ../libs/foo/1.0參數,以及-l foo ,但是沒有-l bar跟蹤!

如果我手動將-l bar添加到cc ,它就可以構建(並運行)。

你能讓我知道我錯過了什么嗎? 我是否應該為libbar創建一個FFI綁定,即使我沒有在Rust中使用它並且它沒有從libfoo的API中公開?

問題是FFI定義中的#[link]屬性與build.rs構建腳本的輸出之間存在沖突。

似乎#[link]屬性指示rustc忽略cargo:rustc-link-*指令。

修復就像刪除#[link]屬性一樣簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM