rust-impl-cross-compile
rust-impl-cross-compile
The cross-compilation skill: how to build a Rust crate for a target other than the host. Covers target triple format and common triples, rustup target add to install standard library artifacts, cargo build --target <triple>, the cross crate for container-based toolchains, conditional compilation via cfg(target_os = ...) and related predicates, the cfg_attr attribute for selective attribute emission, and per-target linker / runner configuration via .cargo/config.toml.
Cross-references: [[rust-impl-cargo-project]] (single-crate Cargo.toml + profiles), [[rust-impl-build-scripts]] (build.rs that detects target via env vars), [[rust-impl-ffi-bindgen]] (cross-compile C deps and adjust sysroot), [[rust-core-toolchain]] (rustup channels, components, target lists).
When to use this skill
- User runs
cargo buildand getslinker 'cc' not foundorerror: target may not be installedafter switching--target. - User wants a portable static Linux binary and asks
muslvsgnu. - User builds for Windows from Linux and asks
pc-windows-msvcvspc-windows-gnu. - User builds for macOS ARM (Apple Silicon) from x86_64 host.
- User asks how to build a
.wasmmodule and askswasm32-unknown-unknownvswasm32-wasip1. - User writes platform-conditional code with
#[cfg(target_os = "...")]orcfg!(...). - User wants a single attribute (e.g.
#[repr(C)],#[link(...)]) to appear only on one target. - User configures a custom linker for a target via
[target.<triple>] linker = "..."in.cargo/config.toml. - User asks about the
crosstool because installing native toolchains is tedious.