Ern*_*est 1 ffi cross-compiling rust musl
我有一个 Rust 库,它为FFI. 我假设我必须设置crate-type为cdylib- 因为我想从 Ruby 和 PHP 调用这些函数(通过ffiruby gem调用)。但是我无法将它从 OSX 交叉编译到 Linux。我试图遵循一些使用的教程musl libc- 这是针对静态库的,但我还没有找到其他任何东西。
所以链接器是这样定义的:
# .cargo/config
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
Run Code Online (Sandbox Code Playgroud)
我试图用以下方法编译它:
cargo build --release --target x86_64-unknown-linux-musl
Run Code Online (Sandbox Code Playgroud)
但是有直接的错误:
error: cannot produce cdylib for `my-crate-name` as the target `x86_64-unknown-linux-musl` does not support these crate types
Run Code Online (Sandbox Code Playgroud)
我的问题是:什么目标/链接器对可用于交叉编译cdylib?为什么 musl 不支持这些 crate 类型?甚至有可能吗?
我有一个 Rust 库,它为
FFI. 因此我必须将 crate-type 设置为cdylib.
你从哪里得到这些信息?您可以创建动态库 ( .so) 或静态库 ( .a)。Alex 有一个包含大量示例的存储库:rust-ffi-examples。
musl用于您确实想要创建二进制文件的情况,该二进制文件是静态链接的并且完全没有依赖关系 ( staticlib)。一切都在二进制里面。你把它扔到 Linux 盒子里,它就会工作。
动态链接用于您知道将满足所有依赖项的情况,您需要更小的二进制文件等 ( cdylib)。但是,您必须确保依赖项确实存在,否则它将无法工作。
我通常不关心交叉编译,因为如果您需要动态链接其他 Linux 库,这可能会非常棘手。对于这些情况,我有:
有很多方法可以实现你想要的。请参阅@Shepmaster 评论:使用 CI 并在某处上传构建工件。
你真的需要交叉编译吗?没有其他方法可以实现您的目标吗?尽可能避免它。
$ brew tap SergioBenitez/osxct
$ brew install x86_64-unknown-linux-gnu
$ rustup target add x86_64-unknown-linux-gnu
Run Code Online (Sandbox Code Playgroud)
将以下行添加到~/.cargo/config:
[target.x86_64-unknown-linux-gnu]
linker = "x86_64-unknown-linux-gnu-gcc"
Run Code Online (Sandbox Code Playgroud)
Cargo.toml 内容:
[package]
name = "sample"
version = "0.1.0"
edition = "2018"
[lib]
crate-type = ["cdylib"]
Run Code Online (Sandbox Code Playgroud)
src/lib.rs 内容:
[target.x86_64-unknown-linux-gnu]
linker = "x86_64-unknown-linux-gnu-gcc"
Run Code Online (Sandbox Code Playgroud)
编译:
[package]
name = "sample"
version = "0.1.0"
edition = "2018"
[lib]
crate-type = ["cdylib"]
Run Code Online (Sandbox Code Playgroud)
检查输出:
#[no_mangle]
pub extern fn hello() {
println!("Rust here");
}
Run Code Online (Sandbox Code Playgroud)
检查库符号:
$ cargo build --release --target x86_64-unknown-linux-gnu
Run Code Online (Sandbox Code Playgroud)
复制target/x86_64-unknown-linux-gnu/release/libsample.so到你的 Linux 机器上。
手动加载
sample.c 内容:
$ file target/x86_64-unknown-linux-gnu/release/libsample.so
target/x86_64-unknown-linux-gnu/release/libsample.so: ELF 64-bit LSB pie executable x86-64, version 1 (SYSV), dynamically linked, with debug_info, not stripped
Run Code Online (Sandbox Code Playgroud)
编译gcc -rdynamic -o sample sample.c -ldl并运行它:
x86_64-unknown-linux-gnu-nm -D target/x86_64-unknown-linux-gnu/release/libsample.so | grep hello
0000000000003900 T hello
Run Code Online (Sandbox Code Playgroud)
检查它是否是动态链接的:
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char**argv) {
void *lib;
void (*hello)(void);
char *error;
lib = dlopen("./libsample.so", RTLD_NOW);
if (!lib) {
fprintf(stderr, "%s\n", dlerror());
exit(-1);
}
dlerror();
*(void **)(&hello) = dlsym(lib, "hello");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
dlclose(lib);
exit(-1);
}
(*hello)();
dlclose(lib);
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
动态链接
sample.c 内容:
$ ./sample
Rust here
Run Code Online (Sandbox Code Playgroud)
编译gcc sample.c -o sample -lsample -L.并运行它:
$ ldd ./sample
linux-vdso.so.1 (0x00007ffe609eb000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc7bdd69000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc7bd978000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc7be16f000)
Run Code Online (Sandbox Code Playgroud)
检查它是否是动态链接的:
extern void hello(void);
int main(int argc, char **argv) {
hello();
}
Run Code Online (Sandbox Code Playgroud)
$ LD_LIBRARY_PATH=. ./sample
Rust here
Run Code Online (Sandbox Code Playgroud)
将以下行添加到~/.cargo/config:
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
Run Code Online (Sandbox Code Playgroud)
Cargo.toml 内容:
[package]
name = "sample"
version = "0.1.0"
edition = "2018"
[lib]
crate-type = ["staticlib"]
Run Code Online (Sandbox Code Playgroud)
src/lib.rs 内容:
$ LD_LIBRARY_PATH=. ldd ./sample
linux-vdso.so.1 (0x00007ffc6fef6000)
libsample.so => ./libsample.so (0x00007f8601ba3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f86017b2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f86015ae000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f86013a6000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8601187000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8600f6f000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8601fd5000)
Run Code Online (Sandbox Code Playgroud)
编译:
$ rustup target add x86_64-unknown-linux-musl
$ brew install filosottile/musl-cross/musl-cross
Run Code Online (Sandbox Code Playgroud)
复制target/x86_64-unknown-linux-musl/release/libsample.a到你的 Linux 机器上。
sample.c 内容:
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
Run Code Online (Sandbox Code Playgroud)
编译gcc sample.c libsample.a -o sample并运行它:
[package]
name = "sample"
version = "0.1.0"
edition = "2018"
[lib]
crate-type = ["staticlib"]
Run Code Online (Sandbox Code Playgroud)
检查它是否是静态链接的:
#![crate_type = "staticlib"]
#[no_mangle]
pub extern fn hello() {
println!("Rust here");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1483 次 |
| 最近记录: |