重复符号:rust_开始_在混合rust和c时展开

1l5u6lss  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(76)

我有以下情况:
我想在我的裸金属微控制器项目中混合使用rust和c,因此我不使用cargo,而是直接调用rustc并在我自己的构建系统中使用它。
我有一个rust库,我只想从c调用它:

#![no_std]
#![no_main]
#![crate_type="lib"]

#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn rust_sample_lib_for_c(a: i8) -> i8 {
    return a+53;
}

字符串
我像这样编译这个库

rustc --target thumbv7em-none-eabihf -C panic=abort -C opt-level=3 --emit obj --crate-type staticlib src/rust_sample_lib_for_c.rs -o src/librust_sample_lib_for_c.a


此外,我还有第二个独立的rust库,类似于上面的那个,它是用相同的rustc命令编译的。
然而,当我想链接c源代码和两个rust库时,我得到以下错误:

ld.lld: error: duplicate symbol: rust_begin_unwind


看起来rust_开始_unwind在两个静态编译库中都有定义。我用objdump查看了库,确实在两个静态编译库中都有rust_开始_unwind符号。
在编译应该从c调用的独立rust库时,如何避免多个定义的rust_开始_unwind符号?
需要在每个库中定义panic handler函数,否则它们无法编译。

rmbxnbpk

rmbxnbpk1#

我不知道如何使用rust工具链来解决这个问题,但是你可以解压缩两个静态库,删除一个Rust运行时库的副本,并使用-r (partial link)参数将它们再次链接到链接器命令行

相关问题