我正在尝试静态链接到用 Rust 编写的库:
#![crate_type = "staticlib"]
#[no_mangle]
pub extern "C" fn foo() {
println!("bork!");
}
Run Code Online (Sandbox Code Playgroud)
在 C 中使用以下代码:
void foo();
int main()
{
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用 rustc 编译 lib:
rustc foo.rs
Run Code Online (Sandbox Code Playgroud)
编译二进制文件并与库链接:
gcc -g bar.c libfoo.a -ldl -lpthread -lrt -lgcc_s -lpthread -lc -lm -o bar
Run Code Online (Sandbox Code Playgroud)
在调试器内运行:
(gdb) run
Starting program: /home/kykc/rusttest/bar
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff72117df in __cxa_thread_atexit_impl (func=<optimized out>, obj=<optimized out>, dso_symbol=0x0) at cxa_thread_atexit_impl.c:67
67 …Run Code Online (Sandbox Code Playgroud)