我正在尝试使用cargo build --target=x86_64-unknown-linux-musl
将我的Mac OS上的Rust项目交叉编译到Linux。
- 我使用
brew install FiloSottile/musl-cross/musl-cross
在Mac上安装了适用于Linux + musl交叉编译的二进制文件,因为我希望得到一个独立的二进制文件。 - 我还使用
rustup target add x86_64-unknown-linux-gnu
安装了目标文件。 - 并且我在
.cargo/config
中设置了以下内容:
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
然而,我在使用两个库sqlite3和pcap时遇到了问题:
= note: /usr/local/Cellar/musl-cross/0.9.8/libexec/bin/../lib/gcc/x86_64-linux-musl/6.4.0/../../../../x86_64-linux-musl/bin/ld: cannot find -lsqlite3
/usr/local/Cellar/musl-cross/0.9.8/libexec/bin/../lib/gcc/x86_64-linux-musl/6.4.0/../../../../x86_64-linux-musl/bin/ld: cannot find -lpcap
collect2: error: ld returned 1 exit status
我通过在Cargo.toml
文件中添加features = ["bundled"]
来解决sqlite3链接器问题(类似于此处所述的方法- https://users.rust-lang.org/t/linker-cannot-find-lsqlite3/23230/18),这可能是从源代码构建的(很好),但当我尝试对pcap进行相同操作时,出现了以下错误:
the package `myProject` depends on `pcap`, with features: `bundled` but `pcap` does not have these features.
在某个地方读到后,我也尝试过(心存希望)使用 RUSTPATH='-L/...' cargo build ...
提供已安装的 Mac 上的 libpcap 路径, 但结果当然是未定义符号错误。
有什么方法可以解决这个问题,并在 macOS 上交叉编译我的 Rust 项目成为静态链接二进制文件以在 Linux 上运行?