[英]compile application with gcc that is different from library compiled with different gcc version
[英]Linking a dynamic library compiled from Rust with gcc
我想從Rust程序構建一個動態庫,並將其鏈接到現有的C ++項目。 對於C ++項目,我們一直堅持使用gcc進行編譯(一個相對較舊的gcc 4.8.2,但我也嘗試使用gcc 7.3.0同樣的問題)。
這是該問題的最小示例:
SRC / lib.rs
#[no_mangle]
pub unsafe extern "C" fn hello() {
println!("Hello World, Rust here!");
}
Cargo.toml
[package]
name = "gcc-linking"
version = "0.1.0"
authors = ..
edition = "2018"
[lib]
crate-type = ["dylib"]
[dependencies]
HELLO.CPP:
extern "C" void hello();
int main() {
hello();
return 0;
}
現在,當我與clang
鏈接時,一切都很好:
cargo build --lib
clang -L target/debug -l gcc_linking hello.cpp -o hello
LD_LIBRARY_PATH=target/debug:$LD_LIBRARY_PATH ./hello
正如所料,這導致:
Hello World, Rust here!
但是,如果我嘗試將其與gcc
鏈接,我會收到以下鏈接錯誤:
gcc -L target/debug -l gcc_linking hello.cpp -o hello
輸出:
/tmp/ccRdGJOK.o: In function `main':
hello.cpp:(.text+0x5): undefined reference to `hello'
collect2: error: ld returned 1 exit status
看動態庫:
# objdump -T output
0000000000043f60 g DF .text 0000000000000043 Base hello
# nm -gC output
0000000000043f60 T hello
我懷疑這個問題與函數名稱的修改有關,但我無法弄清楚如何解決它。
有任何想法嗎?
正如@Jmb建議的那樣,解決方案是將參數的順序更改為gcc
並在C ++文件之后列出共享庫:
gcc -L target/debug hello.cpp -l gcc_linking -o hello
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.