zig-cross
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZig Cross-Compilation
Zig交叉编译
Purpose
用途
Guide agents through Zig's built-in cross-compilation: target triple selection, CPU feature targeting, for cross-compiling C projects, embedded bare-metal targets, and WASM output — all without requiring a system cross-toolchain.
zig cc指导开发者通过Zig内置的交叉编译功能完成操作:包括目标三元组选择、CPU特性指定、使用交叉编译C项目、嵌入式裸机目标平台编译以及WASM输出——所有操作均无需依赖系统交叉工具链。
zig ccTriggers
触发场景
- "How do I cross-compile a Zig program for ARM?"
- "How do I build Zig for a different OS?"
- "How do I use Zig to cross-compile C code for another platform?"
- "How do I build Zig for embedded/bare-metal?"
- "How do I target WebAssembly with Zig?"
- "What Zig target triple do I use for Raspberry Pi?"
- "如何为ARM平台交叉编译Zig程序?"
- "如何为不同操作系统编译Zig程序?"
- "如何使用Zig为其他平台交叉编译C代码?"
- "如何为嵌入式/裸机环境编译Zig程序?"
- "如何使用Zig针对WebAssembly进行编译?"
- "针对树莓派应该使用什么Zig目标三元组?"
Workflow
操作流程
1. Zig's native cross-compilation
1. Zig原生交叉编译
Zig has cross-compilation built in — no cross-toolchain, no Docker, no sysroot needed for pure Zig code:
bash
undefinedZig内置了交叉编译功能——纯Zig代码编译无需交叉工具链、Docker或sysroot:
bash
undefinedList all supported targets
List all supported targets
zig targets | python3 -c "import sys,json; d=json.load(sys.stdin); [print(t) for t in d['libc']]"
zig targets | python3 -c "import sys,json; d=json.load(sys.stdin); [print(t) for t in d['libc']]"
Build for a specific target
Build for a specific target
zig build-exe src/main.zig -target aarch64-linux-gnu -O ReleaseFast
zig build-exe src/main.zig -target aarch64-linux-gnu -O ReleaseFast
With build system (pass target as option)
With build system (pass target as option)
zig build -Dtarget=aarch64-linux-gnu -Doptimize=ReleaseFast
zig build -Dtarget=aarch64-linux-gnu -Doptimize=ReleaseFast
Cross-compile for Windows from Linux/macOS
Cross-compile for Windows from Linux/macOS
zig build-exe src/main.zig -target x86_64-windows-gnu
zig build-exe src/main.zig -target x86_64-windows-gnu
Cross-compile for macOS from Linux (requires macOS SDK)
Cross-compile for macOS from Linux (requires macOS SDK)
zig build-exe src/main.zig -target aarch64-macos-none
undefinedzig build-exe src/main.zig -target aarch64-macos-none
undefined2. Common target triples
2. 常见目标三元组
| Target triple | Platform |
|---|---|
| Linux x86-64 (glibc) |
| Linux x86-64 (musl, static) |
| ARM64 Linux (Pi 4, AWS Graviton) |
| ARM64 Linux static |
| ARM 32-bit Linux (Pi 2/3) |
| Windows x86-64 |
| macOS Apple Silicon |
| macOS Intel |
| WASM (browser, no OS) |
| WASM with WASI |
| Cortex-M3 bare metal |
| Cortex-M4/M7 with FPU |
| RISC-V 32-bit bare metal |
| 目标三元组 | 平台 |
|---|---|
| Linux x86-64(glibc) |
| Linux x86-64(musl,静态链接) |
| ARM64 Linux(树莓派4、AWS Graviton) |
| ARM64 Linux(静态链接) |
| ARM 32位 Linux(树莓派2/3) |
| Windows x86-64 |
| macOS Apple Silicon |
| macOS Intel |
| WASM(浏览器环境,无OS) |
| 带WASI的WASM |
| Cortex-M3裸机 |
| 带FPU的Cortex-M4/M7 |
| RISC-V 32位裸机 |
3. CPU feature targeting
3. CPU特性指定
bash
undefinedbash
undefinedNative CPU (auto-detect, only for native builds)
Native CPU (auto-detect, only for native builds)
zig build-exe src/main.zig -mcpu native
zig build-exe src/main.zig -mcpu native
Baseline for architecture (most compatible)
Baseline for architecture (most compatible)
zig build-exe src/main.zig -target x86_64-linux-gnu -mcpu baseline
zig build-exe src/main.zig -target x86_64-linux-gnu -mcpu baseline
x86-64 with AVX2
x86-64 with AVX2
zig build-exe src/main.zig -target x86_64-linux-gnu -mcpu x86_64+avx2+bmi2
zig build-exe src/main.zig -target x86_64-linux-gnu -mcpu x86_64+avx2+bmi2
Raspberry Pi 4 (Cortex-A72)
Raspberry Pi 4 (Cortex-A72)
zig build-exe src/main.zig -target aarch64-linux-gnu -mcpu cortex_a72
zig build-exe src/main.zig -target aarch64-linux-gnu -mcpu cortex_a72
Cortex-M4 with FPU
Cortex-M4 with FPU
zig build-exe src/main.zig
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
zig build-exe src/main.zig
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
List CPU features for a target
List CPU features for a target
zig targets | python3 -c "
import sys,json
d=json.load(sys.stdin)
for c in d['cpus']:
if 'cortex' in c['name']:
print(c['name'])
"
undefinedzig targets | python3 -c "
import sys,json
d=json.load(sys.stdin)
for c in d['cpus']:
if 'cortex' in c['name']:
print(c['name'])
"
undefined4. zig cc for C cross-compilation
4. 使用zig cc交叉编译C代码
zig ccbash
undefinedzig ccbash
undefinedCross-compile C for ARM64 Linux
Cross-compile C for ARM64 Linux
zig cc -target aarch64-linux-gnu -O2 -o myapp-arm64 main.c
zig cc -target aarch64-linux-gnu -O2 -o myapp-arm64 main.c
Cross-compile C for Windows
Cross-compile C for Windows
zig cc -target x86_64-windows-gnu main.c -o myapp.exe
zig cc -target x86_64-windows-gnu main.c -o myapp.exe
Cross-compile C with static musl linking
Cross-compile C with static musl linking
zig cc -target x86_64-linux-musl -static main.c -o myapp-static
zig cc -target x86_64-linux-musl -static main.c -o myapp-static
Use in Makefile for all platforms
Use in Makefile for all platforms
CC_LINUX_AMD64 = zig cc -target x86_64-linux-gnu
CC_LINUX_ARM64 = zig cc -target aarch64-linux-gnu
CC_WINDOWS = zig cc -target x86_64-windows-gnu
undefinedCC_LINUX_AMD64 = zig cc -target x86_64-linux-gnu
CC_LINUX_ARM64 = zig cc -target aarch64-linux-gnu
CC_WINDOWS = zig cc -target x86_64-windows-gnu
undefined5. Build system cross-compilation
5. 构建系统交叉编译
zig
// build.zig — cross-build all targets
pub fn build(b: *std.Build) void {
const targets = [_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
.{ .cpu_arch = .aarch64, .os_tag = .macos },
};
for (targets) |t| {
const target = b.resolveTargetQuery(t);
const exe = b.addExecutable(.{
.name = b.fmt("myapp-{s}", .{@tagName(t.cpu_arch.?)}),
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .ReleaseFast,
});
b.installArtifact(exe);
}
}bash
undefinedzig
// build.zig — cross-build all targets
pub fn build(b: *std.Build) void {
const targets = [_]std.Target.Query{
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu },
.{ .cpu_arch = .aarch64, .os_tag = .macos },
};
for (targets) |t| {
const target = b.resolveTargetQuery(t);
const exe = b.addExecutable(.{
.name = b.fmt("myapp-{s}", .{@tagName(t.cpu_arch.?)}),
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .ReleaseFast,
});
b.installArtifact(exe);
}
}bash
undefinedBuild all targets
Build all targets
zig build
zig build
Creates: zig-out/bin/myapp-x86_64, myapp-aarch64, myapp-x86_64.exe, myapp-aarch64
Creates: zig-out/bin/myapp-x86_64, myapp-aarch64, myapp-x86_64.exe, myapp-aarch64
undefinedundefined6. Embedded (bare-metal) targets
6. 嵌入式(裸机)目标平台
bash
undefinedbash
undefinedCortex-M4 with FPU (STM32F4xx)
Cortex-M4 with FPU (STM32F4xx)
zig build-exe src/main.zig
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
-O ReleaseSmall
--script linker.ld
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
-O ReleaseSmall
--script linker.ld
```zig
// src/main.zig — bare metal entry point
const std = @import("std");
// Custom panic handler for embedded (no OS)
pub fn panic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
// Toggle LED or halt
while (true) {}
}
// Entry point (matches linker script)
export fn _start() void {
main() catch |err| {
_ = err;
while (true) {}
};
}
fn main() !void {
// Hardware initialization...
}zig build-exe src/main.zig
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
-O ReleaseSmall
--script linker.ld
-target thumbv7em-freestanding-eabihf
-mcpu cortex_m4+vfp4
-O ReleaseSmall
--script linker.ld
```zig
// src/main.zig — bare metal entry point
const std = @import("std");
// Custom panic handler for embedded (no OS)
pub fn panic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
// Toggle LED or halt
while (true) {}
}
// Entry point (matches linker script)
export fn _start() void {
main() catch |err| {
_ = err;
while (true) {}
};
}
fn main() !void {
// Hardware initialization...
}7. WebAssembly
7. WebAssembly
bash
undefinedbash
undefinedWASM freestanding (browser)
WASM freestanding (browser)
zig build-exe src/main.zig
-target wasm32-freestanding
-O ReleaseSmall
--export=init
--export=update
-fno-entry
-target wasm32-freestanding
-O ReleaseSmall
--export=init
--export=update
-fno-entry
zig build-exe src/main.zig
-target wasm32-freestanding
-O ReleaseSmall
--export=init
--export=update
-fno-entry
-target wasm32-freestanding
-O ReleaseSmall
--export=init
--export=update
-fno-entry
WASM WASI (wasmtime, wasmer)
WASM WASI (wasmtime, wasmer)
zig build-exe src/main.zig
-target wasm32-wasi
-O ReleaseSafe wasmtime myapp.wasm
-target wasm32-wasi
-O ReleaseSafe wasmtime myapp.wasm
zig build-exe src/main.zig
-target wasm32-wasi
-O ReleaseSafe wasmtime myapp.wasm
-target wasm32-wasi
-O ReleaseSafe wasmtime myapp.wasm
Optimize WASM size
Optimize WASM size
wasm-opt -Oz myapp.wasm -o myapp.opt.wasm
For target triple reference and embedded linker script setup, see [references/zig-target-triples.md](references/zig-target-triples.md).wasm-opt -Oz myapp.wasm -o myapp.opt.wasm
如需目标三元组参考及嵌入式链接脚本设置,请查看[references/zig-target-triples.md](references/zig-target-triples.md)。Related skills
相关技能
- Use for single-file compilation flags
skills/zig/zig-compiler - Use for multi-target build.zig configuration
skills/zig/zig-build-system - Use for system cross-toolchain setup when needed
skills/compilers/cross-gcc - Use for Rust's cross-compilation approach comparison
skills/rust/rust-cross
- 如需单文件编译参数,请使用
skills/zig/zig-compiler - 如需多目标平台的build.zig配置,请使用
skills/zig/zig-build-system - 如需系统交叉工具链设置,请使用
skills/compilers/cross-gcc - 如需对比Rust的交叉编译方法,请使用
skills/rust/rust-cross