我正在尝试用Rust编译一个static库,然后在我的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 unsafe 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();
}为了构建这个库,我在我的铁锈目录中运行了:
货建库
(一切顺利)我继续运行,在我的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()的未定义引用
发布于 2018-05-08 12:52:23
c++中的名称损坏是不标准化的,因此void hello_world_in_rust()可能与C有不同的链接。通过使用extern "C"作为函数签名/原型的一部分,您可以在两种语言中强制使用相同的C链接:
extern "C" void hello_world_in_rust();https://stackoverflow.com/questions/50229088
复制相似问题