mcpField
R
rust-cross

rust-cross

github.com/mohitmishra786开发269526 次安装18 周前更新
代码仓库

关于 rust-cross

面向编程 Agent 的 Rust 交叉编译技能,介绍目标平台配置及多种构建路径。内容涵盖 cross、cargo-zigbuild、静态构建和嵌入式 bare-metal 目标。

主要功能

  1. 01添加 rustup 编译目标
  2. 02使用 cross 进行容器化构建
  3. 03使用 cargo-zigbuild 交叉编译
  4. 04配置 .cargo/config.toml
  5. 05构建 musl 静态二进制文件
  6. 06配置嵌入式 bare-metal 目标

适用场景

  1. 01为 ARM 或 aarch64 构建 Rust 程序
  2. 02生成面向其他操作系统的二进制文件
  3. 03构建 no_std 嵌入式程序
  4. 04排查交叉编译二进制无法运行的问题

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill rust-cross

安装后在 Claude Code 中输入/rust-cross确认已加载。

附带文件references/cross-targets.md

详细介绍

Rust 交叉编译

用途

指导 Agent 进行 Rust 交叉编译:添加 rustup 目标、使用 cross 进行基于 Docker 的隔离交叉构建、使用 cargo-zigbuild 实现零配置交叉编译、配置 .cargo/config.toml,以及构建嵌入式裸机目标。

触发条件

  • “如何为 ARM/aarch64 交叉编译 Rust?”
  • “如何为其他操作系统构建 Rust 二进制文件?”
  • “如何使用 cross 工具进行 Rust 交叉编译?”
  • “如何为嵌入式(no_std)目标构建 Rust?”
  • “如何使用 cargo-zigbuild?”
  • “我的 Rust 交叉编译二进制文件无法在目标设备上运行”

工作流

1. 添加 rustup 目标
# List installed targets
rustup target list --installed

# List all available targets
rustup target list

# Add a target
rustup target add aarch64-unknown-linux-gnu
rustup target add x86_64-unknown-linux-musl   # static Linux
rustup target add wasm32-unknown-unknown       # WASM
rustup target add thumbv7m-none-eabi          # Cortex-M

# Build for target
cargo build --target aarch64-unknown-linux-gnu --release
2. 常见目标三元组
目标用途
x86_64-unknown-linux-gnuLinux x86-64(glibc)
x86_64-unknown-linux-muslLinux x86-64(musl,静态)
aarch64-unknown-linux-gnuARM64 Linux(Raspberry Pi 4、AWS Graviton)
aarch64-unknown-linux-muslARM64 Linux 静态构建
x86_64-pc-windows-gnuWindows x86-64(MinGW)
x86_64-pc-windows-msvcWindows x86-64(MSVC)
x86_64-apple-darwinmacOS x86-64
aarch64-apple-darwinmacOS Apple Silicon
wasm32-unknown-unknownWASM(浏览器)
wasm32-wasi使用 WASI 的 WASM
thumbv7m-none-eabiCortex-M3 裸机
thumbv7em-none-eabihf带 FPU 的 Cortex-M4/M7
riscv32imac-unknown-none-elfRISC-V 32 位裸机
3. cross 工具(基于 Docker,最简单)

cross 使用预构建的 Docker 镜像,其中包含正确的交叉工具链:

# Install
cargo install cross

# Build (drop-in replacement for cargo)
cross build --target aarch64-unknown-linux-gnu --release
cross test --target aarch64-unknown-linux-gnu

# Cross.toml — project configuration
# Cross.toml
[target.aarch64-unknown-linux-gnu]
image = "ghcr.io/cross-rs/aarch64-unknown-linux-gnu:main"
pre-build = [
    "apt-get update && apt-get install -y libssl-dev:arm64"
]

[build.env]
passthrough = ["PKG_CONFIG_PATH", "OPENSSL_DIR"]
4. cargo-zigbuild(零配置,使用 zig cc)

zig cc 附带完整的 C 交叉工具链,无需系统交叉编译器:

# Install
cargo install cargo-zigbuild
# Also needs zig installed: https://ziglang.org/download/

# Build (no Docker, no system cross-compiler)
cargo zigbuild --target aarch64-unknown-linux-gnu --release
cargo zigbuild --target x86_64-unknown-linux-musl --release

# Target with glibc version (important for compatibility)
cargo zigbuild --target aarch64-unknown-linux-gnu.2.17 --release
# This builds against glibc 2.17 (very compatible)

# Windows from Linux/macOS
cargo zigbuild --target x86_64-pc-windows-gnu --release

与 cross 相比,cargo-zigbuild 的优势:

  • 无需 Docker
  • 速度更快(无需启动容器)
  • 开箱即用,支持大多数目标
  • 支持精确指定 glibc 版本
5. 用于交叉目标的 .cargo/config.toml
# .cargo/config.toml

[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"    # System cross-linker
# or with zig:
# linker = "zig"
# rustflags = ["-C", "link-arg=cc", "-C", "link-arg=-target", "-C", "link-arg=aarch64-linux-gnu"]

[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
rustflags = ["-C", "target-feature=+crt-static"]

[target.wasm32-unknown-unknown]
runner = "wasmtime"    # Run WASM tests with wasmtime

[target.thumbv7m-none-eabi]
runner = "qemu-arm -cpu cortex-m3"
6. 使用 musl 构建静态二进制文件
# Add musl target
rustup target add x86_64-unknown-linux-musl

# Build statically linked binary
cargo build --target x86_64-unknown-linux-musl --release

# Verify it's static
file target/x86_64-unknown-linux-musl/release/myapp
# → ELF 64-bit, statically linked, not stripped

# Or with cargo-zigbuild (easier musl)
cargo zigbuild --target x86_64-unknown-linux-musl --release
7. 嵌入式裸机(#[no_std])
# .cargo/config.toml
[build]
target = "thumbv7em-none-eabihf"   # Set default target

[target.'cfg(target_arch = "arm")']
runner = "probe-run --chip STM32F411CE"
// src/main.rs
#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[cortex_m_rt::entry]
fn main() -> ! {
    loop {}
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}
# Cargo.toml
[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.7"

[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
cargo build --release  # Uses default target from .cargo/config.toml

有关目标三元组参考和嵌入式配置的详情,请参阅 [references/]。

相关技能

  • 使用 skills/rust/rustc-basics 配置编译器和构建配置
  • 使用 skills/compilers/cross-gcc 配置底层交叉编译器
  • 使用 skills/zig/zig-cross 了解 Zig 的原生交叉编译方案
  • 当 Rust 属于 CMake 交叉构建的一部分时,使用 skills/build-systems/cmake

评论

支持 Markdown