如何在macOS中编译Rust的no_mangle函数调用C函数

问题描述

我写了下面的Rust函数,可以从C调用C函数

hasRoles

我在macOS中编译了代码,然后……

#![crate_type = "cdylib"]
#[no_mangle]
pub fn foo() {
    unsafe {
        bar();
    };
}

extern "C" {
    pub fn bar();
}

这是在macOS中发生的,但在Linux中没有。

如何在macOS中编译代码

我的clang版本是$ rustc src/lib.rs error: linking with `cc` Failed: exit code: 1 | = note: "cc" … = note: Undefined symbols for architecture x86_64: "_bar",referenced from: _foo in lib.lib.3a1fbbbh-cgu.0.rcgu.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command Failed with exit code 1 (use -v to see invocation) error: aborting due to prevIoUs error


编辑:

我想像这样在C语言中使用上述Rust函数

clang-1103.0.32.62

因此,我想我无法为extern void foo(); int main() { foo(); } void bar() { } 指定#[link(...)]。

(也就是说,我想在Rust中为C程序编写一个插件。)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)