我正在尝试编译一个static
Rust 中的库,然后在我的 C++ 代码中使用它(注意这是从 C++ 调用 Rust,而不是相反)。我浏览了我可以在网上找到的所有教程,并回答了类似的问题,我显然做错了什么,尽管我看不出是什么。
我为我的问题创建了一个最小的例子:
1.Cargo.toml:
[package]
name = "hello_world"
version = "0.1.0"
[lib]
name = "hello_in_rust_lib"
path = "src/lib.rs"
crate-type = ["staticlib"]
[dependencies]
2.lib.rs:
#[no_mangle]
pub extern "C" fn hello_world_in_rust() {
println!("Hello World, Rust here!");
}
3.hello_world_in_cpp.cpp:
extern void hello_world_in_rust();
int main() {
hello_world_in_rust();
}
为了构建库,在我的 rust 目录中运行:
货物构建--lib
(一切顺利)
我继续在我的 C++ 文件夹中运行:
clang++ hello_world_in_cpp.cpp -o hello.out -L ../hello_world/target/release/ -lhello_in_rust_lib
这导致了以下错误:
/tmp/hello_world_in_cpp-cf3577.o:在函数中main
:
hello_world_in_cpp.cpp:(.text+0x5): 未定义的引用hello_world_in_rust()
名称修改c++ /questions/tagged/c%2b%2b没有标准化,因此void hello_world_in_rust()
与以下相比可能有不同的联系c /questions/tagged/c。您可以使用以下命令在两种语言中强制使用相同的 C 链接extern "C"
作为函数签名/原型的一部分:
extern "C" void hello_world_in_rust();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)