c++ - 编译 Rust 静态库并在 C++ 中使用它:未定义的引用
问题描述
我正在尝试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 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();
}
要构建库,在我的 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()
解决方案
推荐阅读
- php - 如何显示表格中每个数据的图像
- c - 为什么当我的输入长度为 8 个或更多字符时,打印 8 个字符后出现符号?
- php - php邮件未收到表单数据
- ios - 无法将 xcassets 分配给 Xcode 11 上的主要目标
- python - 如何从 python 中的服务器更新客户端标签?
- python - 如何通过逻辑回归应用 Leave one out 交叉验证并找到系数的值?
- reactjs - 找不到模块:无法解析“反应”
- c# - 将文本的特定部分子串到对象列表的最有效方法是什么
- firebase - 我可以使用 firebase_functions_interop 从 Cloud Function 对 Firestore 执行“集合组查询”吗?
- python - 我不明白 index() 线是如何工作的