Windows主机使用cross-rs编译MSVC失败,求解决方案
问题
我正在搭建CI/CD流水线,需要为Windows、MacOS、Linux的amd64和arm64架构编译二进制文件,采用GitHub Actions,遵循「对应宿主系统构建目标平台文件」的原则(Linux构建Linux文件,Windows构建Windows文件,MacOS构建MacOS文件)。
目前Linux构建正常,但Windows无法编译Windows arm64和amd64目标文件。我知道Windows amd64无法直接构建Windows arm64,打算用cross-rs工具,但没找到在Windows主机直接编译的方法。
由于许可问题,cross-rs没有包含Windows和MacOS的预构建镜像,需要通过cross-toolchains添加目标,其中包含需要单独构建的Dockerfile,且要合法获取msvc编译器工具链。
我按照相关文档操作后失败,使用GitHub Actions的windows-latest宿主,构建脚本如下:
curl https://sh.rustup.rs -sSf | sh -s -- -y cargo install cross git clone https://github.com/cross-rs/cross /cross cd /cross git submodule update --init --remote # 我试过加和不加下面这行 cargo xtask configure-crosstool x86_64-pc-windows-msvc cargo build-docker-image x86_64-pc-windows-msvc --tag local # 从未执行到这里 echo '\n' >> Cargo.toml echo '[target.x86_64-pc-windows-msvc]\n' >> Cargo.toml echo 'image = "ghcr.io/cross-rs/x86_64-pc-windows-msvc:local"\n' >> Cargo.toml cross run --release --target x86_64-pc-windows-msvc
执行后报错:
Run git clone https://github.com/cross-rs/cross /cross Cloning into '/cross'... Submodule 'docker/cross-toolchains' (https://github.com/cross-rs/cross-toolchains.git) registered for path 'docker/cross-toolchains' Cloning into 'D:/cross/docker/cross-toolchains'... Submodule path 'docker/cross-toolchains': checked out '4299096a1468f91aa9a7430e4763de685cb386a1' Updating crates.io index Downloading crates ... Downloaded same-file v1.0.6 Downloaded thiserror-impl v1.0.40 .... .... .... Compiling wildmatch v2.1.1 Compiling xtask v0.0.0-dev.0 (D:\cross\xtask) Finished dev [unoptimized + debuginfo] target(s) in 1m 56s Running `target\debug\xtask.exe configure-crosstool x86_64-pc-windows-msvc` Error: 0: unable to find config for target "x86_64-pc-windows-msvc" Location: xtask\src\crosstool.rs:73 Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it. Run with RUST_BACKTRACE=full to include source snippets. error: process didn't exit successfully: `target\debug\xtask.exe configure-crosstool x86_64-pc-windows-msvc` (exit code: 1) Finished dev [unoptimized + debuginfo] target(s) in 0.21s Running `target\debug\xtask.exe build-docker-image x86_64-pc-windows-msvc` Error: 0: unable to find dockerfile for target "x86_64-pc-windows-msvc" Location: xtask\src\build_docker_image.rs:105 Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it. Run with RUST_BACKTRACE=full to include source snippets. error: process didn't exit successfully: `target\debug\xtask.exe build-docker-image x86_64-pc-windows-msvc` (exit code: 1) Error: Process completed with exit code 1.
想请教三个问题:
- 我遗漏了什么步骤?
- 能否跳过cross-rs直接编译Windows arm64文件?
- MacOS是否支持跨架构编译:从amd64 Intel Mac主机构建arm64和amd64文件(反之亦然)?
回答
1. cross-rs构建失败的原因及修复
你当前的核心操作错误在于:cross-rs的xtask configure-crosstool和build-docker-image命令需要在cross-toolchains仓库而非cross主仓库中执行,你克隆的是cross主仓库,自然找不到对应目标的配置和Dockerfile。
正确步骤如下:
- 克隆cross-toolchains仓库,而非cross主仓库
- 在cross-toolchains目录下执行xtask相关命令
- Windows环境下用bash脚本执行curl安装rustup存在兼容性问题,建议改用GitHub Actions自带的rust安装方式
修正后的核心脚本片段:
# 安装xtask依赖 git clone https://github.com/cross-rs/cross-toolchains /cross-toolchains cd /cross-toolchains git submodule update --init --remote cargo install --path xtask # 配置并构建Windows arm64目标镜像 cargo xtask configure-crosstool aarch64-pc-windows-msvc cargo xtask build-docker-image aarch64-pc-windows-msvc --tag local
另外注意:cross-rs在Windows主机上使用Docker需要配置WSL2后端,GitHub Actions的windows-latest默认已启用,但需确保Docker服务正常启动。
2. 跳过cross-rs直接编译Windows arm64
完全可以直接编译,无需依赖cross-rs。只需安装对应的Rust目标和MSVC交叉编译工具链:
在GitHub Actions的Windows runner上执行:
# 添加Windows arm64目标 rustup target add aarch64-pc-windows-msvc # 安装Visual Studio的arm64编译工具链 choco install visualstudio2022-workload-vctools --package-parameters "--includeOptional --add Microsoft.VisualStudio.Component.VC.Tools.ARM64" # 直接编译目标架构二进制 cargo build --release --target aarch64-pc-windows-msvc
也可以用GitHub Actions的actions/setup-msbuild动作配置工具链,配合rustup目标完成编译,流程更简洁。
3. MacOS跨架构编译支持
MacOS完全支持跨架构编译:
- Intel Mac(amd64):可直接构建amd64和arm64的MacOS二进制文件,只需添加对应Rust目标:
系统会自动调用Xcode的交叉编译工具链,无需额外配置。rustup target add aarch64-apple-darwin cargo build --release --target aarch64-apple-darwin - Apple Silicon Mac(arm64):同理可构建amd64目标:
编译完成后,还可以用rustup target add x86_64-apple-darwin cargo build --release --target x86_64-apple-darwinlipo工具合并两个架构的二进制文件,生成通用二进制包。
内容的提问来源于stack exchange,提问作者David Alsh

