使用同一个Docker镜像交叉编译Rust程序,支持Linux(musl libc)和macOS(osxcross)。
]([] 。
它包含交叉编译Rust项目所需的必要工具,例如通过musl-libc / musl-gcc生成Linux静态二进制文件(x86_64-unknown-linux-musl),以及通过osxcross生成macOS二进制文件(x86_64-apple-darwin),均通过同一个Linux镜像实现。
该Docker镜像支持多架构(amd64和arm64),可在原生环境中使用。此外,还可通过x86_64 Docker镜像变体交叉编译arm64架构的Linux或Darwin应用。
默认工作目录为/root/src。
以下是Docker镜像中包含的默认工具链。
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target x86_64-unknown-linux-musl"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target x86_64-unknown-linux-gnu"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target x86_64-apple-darwin"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target aarch64-unknown-linux-gnu"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target aarch64-unknown-linux-musl"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target aarch64-apple-darwin"
已知CARGO_HOME默认指向$HOME/.cargo(此处为/root/.cargo)。若要使用自定义Cargo主目录,需确保在交叉编译前将Cargo config文件复制到目标目录,例如执行cp "$HOME/.cargo/config" "$CARGO_HOME/"。否则,可能会遇到链接错误(例如交叉编译至x86_64-apple-darwin目标时)。
也可将该镜像用作Dockerfile的基础镜像:
DockerfileFROM joseluisq/rust-linux-darwin-builder:1.68.1
还可将o32-clang(++)和o64-clang(++)用作常规编译器。
注意:
示例:
sh使用示例: 示例1:CC=o32-clang ./configure --host=i386-apple-darwin21.4 示例2:CC=i386-apple-darwin21.4-clang ./configure --host=i386-apple-darwin21.4 示例3:o64-clang -Wall test.c -o test 示例4:x86_64-apple-darwin21.4-strip -x test !!! 使用Automake时,需用aarch64-apple-darwin21.4-*替代arm64-* !!! !!! CC=aarch64-apple-darwin21.4-clang ./configure --host=aarch64-apple-darwin21.4 !!! !!! CC="aarch64-apple-darwin21.4-clang -arch arm64e" ./configure --host=aarch64-apple-darwin21.4 !!!
以下是使用Makefile交叉编译Rust应用的简单示例。
注意:
/root/src。创建Makefile:
shcompile: @docker run --rm -it \ -v $(PWD):/drone/src \ -w /drone/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ make cross-compile .PHONY: compile cross-compile: @echo @echo "1. 交叉编译示例..." @rustc -vV @echo @echo "2. 编译应用(linux-musl x86_64)..." @cargo build --manifest-path=tests/hello-world/Cargo.toml --release --target x86_64-unknown-linux-musl @du -sh tests/hello-world/target/x86_64-unknown-linux-musl/release/helloworld @echo @echo "3. 编译应用(apple-darwin x86_64)..." @cargo build --manifest-path=tests/hello-world/Cargo.toml --release --target x86_64-apple-darwin @du -sh tests/hello-world/target/x86_64-apple-darwin/release/helloworld .PHONY: cross-compile
运行makefile的compile目标,将生成两个发布版本二进制文件:x86_64-unknown-linux-musl和x86_64-apple-darwin。
shmake compile # 1. 交叉编译示例... # rustc 1.68.1 (8460ca823 2023-03-20) # binary: rustc # commit-hash: 8460ca823e8367a30dda430efda790588b8c84d3 # commit-date: 2023-03-20 # host: aarch64-apple-darwin # release: 1.68.1 # LLVM version: 15.0.6 # 2. 编译应用(linux-musl x86_64)... # Finished release [optimized] target(s) in 0.01s # 1.2M tests/hello-world/target/x86_64-unknown-linux-musl/release/helloworld # 3. 编译应用(apple-darwin x86_64)... # Finished release [optimized] target(s) in 0.01s # 240K tests/hello-world/target/x86_64-apple-darwin/release/helloworld
更多详情可参考James Waples的文章《从Linux交叉编译Rust至macOS》。
详见joseluisq/rust-linux-darwin-builder#7
若某些crate需要C绑定且编译或链接出错,尝试使用Clang进行C/C++构建。
例如交叉编译至macOS:
shCC=o64-clang \ CXX=o64-clang++ \ cargo build --target x86_64-apple-darwin
本项目
v1.42.0及之前版本使用旧版OpenSSLv1.0.2。
_自v1.43.x及更高版本起,使用OpenSSLv1.1.1(LTS),支持至2023-09-11。
详见[***]
除非您明确声明,否则您有意提交的任何贡献(定义于Apache-2.0许可中)将按以下双重许可分发,无额外条款或条件。
欢迎提交Pull request或提交issue。
本作品主要根据MIT许可和Apache许可(版本2.0)的条款分发。
© 2019-present Jose Quintana


来自真实用户的反馈,见证轩辕镜像的优质服务
免费版仅支持 Docker Hub 加速,不承诺可用性和速度;专业版支持更多镜像源,保证可用性和稳定速度,提供优先客服响应。
免费版仅支持 docker.io;专业版支持 docker.io、gcr.io、ghcr.io、registry.k8s.io、nvcr.io、quay.io、mcr.microsoft.com、docker.elastic.co 等。
当返回 402 Payment Required 错误时,表示流量已耗尽,需要充值流量包以恢复服务。
通常由 Docker 版本过低导致,需要升级到 20.x 或更高版本以支持 V2 协议。
先检查 Docker 版本,版本过低则升级;版本正常则验证镜像信息是否正确。
使用 docker tag 命令为镜像打上新标签,去掉域名前缀,使镜像名称更简洁。
探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
在 Linux 系统配置镜像加速服务
在 Docker Desktop 配置镜像加速
Docker Compose 项目配置加速
Kubernetes 集群配置 Containerd
在宝塔面板一键配置镜像加速
Synology 群晖 NAS 配置加速
飞牛 fnOS 系统配置镜像加速
极空间 NAS 系统配置加速服务
爱快 iKuai 路由系统配置加速
绿联 NAS 系统配置镜像加速
QNAP 威联通 NAS 配置加速
Podman 容器引擎配置加速
HPC 科学计算容器配置加速
ghcr、Quay、nvcr 等镜像仓库
无需登录使用专属域名加速
需要其他帮助?请查看我们的 常见问题 或 官方QQ群: 13763429