rust - 跨平台编译
cargo-zigbuild
https://github.com/rust-cross/cargo-zigbuild
https://hub.docker.com/r/messense/cargo-zigbuild/tags
$ docker run --rm -it -v $(pwd):/io -w /io \
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/messense/cargo-zigbuild:main \
cargo zigbuild --release --target x86_64-apple-darwin
$ docker run --rm -it -v $(pwd):/io -w /io \
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/messense/cargo-zigbuild:main \
/bin/sh
cargo zigbuild --release --target x86_64-apple-darwin
cargo zigbuild --release --target aarch64-apple-darwin
cargo zigbuild --release --target x86_64-pc-windows-gnu
cargo zigbuild --release --target x86_64-unknown-linux-gnu安装zig
pip3 install ziglang
安装Rust目标
rustup target add aarch64-unknown-linux-gnu
运行zigbuild
cargo zigbuild --target aarch64-unknown-linux-gnucargo-zigbuild中包含的镜像
RUN rustup target add \
x86_64-unknown-linux-gnu \
x86_64-unknown-linux-musl \
aarch64-unknown-linux-gnu \
aarch64-unknown-linux-musl \
arm-unknown-linux-gnueabihf \
arm-unknown-linux-musleabihf \
x86_64-apple-darwin \
aarch64-apple-darwin \
x86_64-pc-windows-gnu \
aarch64-pc-windows-gnullvmhttps://jishuzhan.net/article/2033803832015257601
https://juejin.cn/post/7325645956254351360
cross完全兼容Cargo命令行参数,主要差异是增加了--target指定目标平台:
功能 命令示例 构建项目 cross build --target x86_64-pc-windows-gnu 运行程序 cross run --target aarch64-unknown-linux-gnu 执行测试 cross test --target riscv64gc-unknown-linux-gnu 发布构建 cross build --release --target x86_64-unknown-linux-musl 代码检查 cross clippy --target armv7-unknown-linux-gnueabihf
利用 cross 工具简化跨平台编译流程
使用 cross 实现 Docker 容器内编译
cross-rs/cross 是一个通过 Docker 容器技术优化跨平台编译过程的强大工具。其工作原理概括如下:
- 准备容器环境: 根据目标平台(Linux 或 Windows),
cross自动下载或创建包含对应平台构建工具链、Rust 工具和必要库的 Docker 镜像。 - 挂载源代码至容器: 当运行
cross命令时,会自动将本地项目源码目录映射到 Docker 容器内部。 - 容器内部编译:
cross根据目标平台在容器内部调用相应的 Rust 工具链进行编译。 - 同步结果至主机: 编译完成后,生成的二进制文件存储于容器内的特定路径,并通过 Docker 卷映射功能回传至宿主 macOS 系统
使用 cross 进行编译
通过 Cargo 安装 cross:
cargo install cross --git https://github.com/cross-rs/cross然后,在项目根目录下配置 Cross.toml 文件,考虑到国内网络问题,可以自定义镜像地址:
[target.x86_64-unknown-linux-gnu]
xargo = false
image = "togettoyou/ghcr.io.cross-rs.x86_64-unknown-linux-gnu:main"
[target.x86_64-pc-windows-gnu]
xargo = false
image = "togettoyou/ghcr.io.cross-rs.x86_64-pc-windows-gnu:main"最后,运行 cross 命令编译 Windows 版本应用:
cross build --release --target x86_64-pc-windows-gnu成功编译后,可在项目 target 目录下找到相应平台的可执行文件。
作者:红尘散仙 链接:https://juejin.cn/post/7325645956254351360 来源:稀土掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
cargo build --target=x86_64-pc-windows-msvc
cargo build --target=x86_64-apple-darwin
cargo build --target=x86_64-unknown-linux-gnuhttps://juejin.cn/post/7325645956254351360
针对 Windows 平台,我们需要借助 cargo-xwin 工具及 LLVM 组件
error: no such command: `xwin`
help: a command with a similar name exists: `fix`
help: view all installed commands with `cargo --list`
help: find a package to install `xwin` with `cargo search cargo-xwin`解决
cargo install cargo-xwin没有配置目标平台
error[E0463]: can't find crate for `core`
|
= note: the `x86_64-pc-windows-msvc` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-pc-windows-msvc`
For more information about this error, try `rustc --explain E0463`解决
#安装目标平台 x86_64-pc-windows-msvc
$ rustup target add x86_64-pc-windows-msvc
#验证目标是否已安装
$ rustup target list --installed --- stderr
error occurred in cc-rs: failed to find tool "llvm-lib": No such file or directory (os error 2)brew install llvm使用cross报错
$ CROSS_CONTAINER_ENGINE=docker cross build --target x86_64-pc-windows-gnu
error: toolchain 'stable-x86_64-unknown-linux-gnu' may not be able to run on this system
note: to build software for that platform, try `rustup target add x86_64-unknown-linux-gnu` instead
note: add the `--force-non-host` flag to install the toolchain anyway
Error:
0: couldn't install toolchain `stable-x86_64-unknown-linux-gnu`
1: `rustup toolchain add stable-x86_64-unknown-linux-gnu --profile minimal` failed with exit status: 1