PS D:\code\leetcode\ffi\c> rustc --crate-type=dylib add.rs --target=x86_64-pc-windows-gnu
error[E0463]: can't find crate for `std`
|
= note: the `x86_64-pc-windows-gnu` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-pc-windows-gnu`
= help: consider building the standard library from source with `cargo build -Zbuild-std`
安装这个倒是不麻烦,但是我已经安装了dev-cpp的mingw,好像有点冲突,所有这些操作都不顺利。但其实过程都是一样的,因为rustc或者cargo编译的时候可以指定target为gnu库可用的二进制
2.下面讨论msvc生成动态库给rust调用
2.1以动态库的形式调用
add.cpp
#include<stdio.h>extern"C" {
__declspec(dllexport) intadd(int a, int b){
return a + b;
}
__declspec(dllexport) voidhello_world(){
printf("hello world!\n");
}
}
以extern "C" 包裹,可以让名称不会发生变化
D:\code\leetcode\ffi\ctor>cl /LD add.cpp
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.41.34120 版
版权所有(C) Microsoft Corporation。保留所有权利。
add.cpp
Microsoft (R) Incremental Linker Version 14.41.34120.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:add.dll
/dll
/implib:add.lib
add.obj
正在创建库 add.lib 和对象 add.exp
ls
add.cpp add.dll add.exp add.lib add.obj
生成的dll和lib需要用到
// add.cpp#include<stdio.h>extern"C" {
intadd(int a, int b){
return a + b;
}
voidhello_world(){
printf("hello world!\n");
}
}
//不需要windows下那些修饰了
g++ -fPIC -shared -o libadd.so add.cpp
编译链接成动态库
root@ubuntu:~/libtest/ctor/r# ls
Cargo.toml add.cpp libadd.so src
Linux下加载库就需要麻烦一些了
首先提示
ie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
= note: /usr/bin/ld: cannot find -ladd: No such file or directory
collect2: error: ld returned 1 exit status
error: could not compile `r1` (bin "r1") due to 1 previous error
这个问题,添加build.rs可以解决,
随后提示
root@ubuntu:~/libtest/ctor/r# cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/r`
target/debug/r: error while loading shared libraries: libadd.so: cannot open shared object file: No such file or directory
root@ubuntu:~/libtest/ctor/r# pwd
/root/libtest/ctor/r
root@ubuntu:~/libtest/ctor/r# ls
Cargo.lock Cargo.toml add.cpp build.rs libadd.so src target
root@ubuntu:~/libtest/ctor/r# cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/r`
The result is: 30
hello world!
root@ubuntu:~/libtest/ctor/r#cargo build --release
新开一个窗口进入release,还是需要设置一下那个环境变量
root@ubuntu:~/libtest/ctor/r/target/release# cp /root/libtest/ctor/r/libadd.so ./
root@ubuntu:~/libtest/ctor/r/target/release# ls
build deps examples incremental libadd.so r r.d
root@ubuntu:~/libtest/ctor/r/target/release# ./r
./r: error while loading shared libraries: libadd.so: cannot open shared object file: No such file or directory
root@ubuntu:~/libtest/ctor/r/target/release# export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
root@ubuntu:~/libtest/ctor/r/target/release# ./r
The result is: 30
hello world!
root@ubuntu:~/libtest/ctor/r/target/release#
1.2静态调用,静态编译
root@ubuntu:~/libtest/ctor/r1# g++ -c add.cpp -o add.o
root@ubuntu:~/libtest/ctor/r1# ar rcs libadd.a add.o
root@ubuntu:~/libtest/ctor/r1# ls
Cargo.toml add.cpp add.o libadd.a src
没有build.rs,直接cargo run试试
ie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
= note: /usr/bin/ld: cannot find -ladd: No such file or directory
collect2: error: ld returned 1 exit status
error: could not compile `r1` (bin "r1") due to 1 previous error
还是和之前一样的错误,
vim build.rs
fn main() {
// 指定库的路径
// println!("cargo:rustc-link-lib=add");
println!(r"cargo:rustc-link-search=native=/root/libtest/ctor/r1");
}
然后cargo run
cargo build --release
都是可以正常生成的,不需要设置LD_LIBRARY_PATH
为了测试,新开一个窗口,没有设置任何环境变量。
root@ubuntu:~/libtest/ctor/r1/target/release# pwd
/root/libtest/ctor/r1/target/release
root@ubuntu:~/libtest/ctor/r1/target/release# ls
build deps examples incremental r1 r1.d
root@ubuntu:~/libtest/ctor/r1/target/release# ./r1
The result is: 30
hello world!
root@ubuntu:~/libtest/rtoc/c# pwd
/root/libtest/rtoc/c
root@ubuntu:~/libtest/rtoc/c# ls
add.rs main.cpp
接下来,进行编译链接的部分
root@ubuntu:~/libtest/rtoc/c# rustc --crate-type=dylib add.rs
root@ubuntu:~/libtest/rtoc/c# ls
add.rs libadd.so main.cpp
root@ubuntu:~/libtest/rtoc/c# g++ main.cpp -L./ -ladd -o main -Wl,-rpath=./
root@ubuntu:~/libtest/rtoc/c# ls
add.rs libadd.so main main.cpp
root@ubuntu:~/libtest/rtoc/c# ./main
Hello from Rust!
The result of adding 3 and 5 is: 8
root@ubuntu:~/libtest/rtoc/c# cp ./main /root/libtest/
root@ubuntu:~/libtest/rtoc/c# cp libadd.so /root/libtest/
root@ubuntu:~/libtest/rtoc/c# 复制库和可执行文件到同意目录,都可以正常执行
root@ubuntu:~/libtest# pwd
/root/libtest
root@ubuntu:~/libtest# ls
ctor libadd.so main rtoc
root@ubuntu:~/libtest# ./main
Hello from Rust!
The result of adding 3 and 5 is: 8
确实是可以正常调用的,只要动态库和可执行文件再相同目录下
root@ubuntu:~/libtest/rtoc/c1# ls
add.rs libadd.a main.cpp
root@ubuntu:~/libtest/rtoc/c1# g++ main.cpp ./libadd.a -o c1
root@ubuntu:~/libtest/rtoc/c1# ls
add.rs c1 libadd.a main.cpp
root@ubuntu:~/libtest/rtoc/c1# ./c1
Hello from Rust!
The result of adding 3 and 5 is: 8
root@ubuntu:~/libtest/rtoc/c1# cp c1 /root/libtest/
root@ubuntu:~/libtest/rtoc/c1# /root/libtest/c1
Hello from Rust!
The result of adding 3 and 5 is: 8
root@ubuntu:~/libtest/rtoc/c1#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探