Macosx rust交叉编译到Linux步骤及常见问题

2023-11-19

安装musl-cross

brew install FiloSottile/musl-cross/musl-cross
sudo ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc

遇到问题

==> make install
Error: gettext: unknown or unsupported macOS version: :dunno

解决办法

brew update-reset

.cargo/config 增加内容

[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

如果没有这个文件新增即可。

增加target:x86_64-unknown-linux-musl

rustup target add x86_64-unknown-linux-musl

可以开心的开始编译了

cargo build --release --target x86_64-unknown-linux-musl

遇到问题

error: failed to run custom build command for `openssl-sys v0.9.95`

解决问题

brew install openssl
export OPENSSL_DIR=/opt/homebrew/opt/openssl@3

又遇到问题

reqwest.38a55170626eab39-cgu.02:(.text._ZN4core3ptr44drop_in_place$LT$reqwest..connect..Inner$GT$17h0198ea10d7817d8cE+0x38): undefined reference to `SSL_CTX_free'

解决问题,需要在Cargo.toml增加openssl的依赖

openssl = { version = "0.10", features = ["vendored"] }

Done


若需评论,请科学上网。