Zig Cross

mohitmishra786/low-level-dev-skills/skills/zig/zig-cross

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

Zig cross-compilation skill. Use when cross-compiling Zig programs to different targets, using Zig's built-in cross-compilation for embedded, WASM, Windows, ARM, or using zig cc to cross-compile C code without a system cross-toolchain. Activates on queries about Zig cross-compilation, zig target triples, zig cc cross-compile, Zig embedded targets, or Zig WASM.

AI 產生的概覽

指導 Zig 交叉編譯:目標三元組、CPU 特性、以 zig cc 編譯 C、嵌入式與 WASM 目標。

功能
此技能提供使用 Zig 內建交叉編譯功能交叉編譯 Zig 程式與 C 程式的說明,不需要系統交叉工具鏈。內容涵蓋目標三元組選擇、CPU 特性指定、以 zig cc 交叉編譯 C 專案、裸機嵌入式目標以及 WebAssembly 輸出。也展示用於一次建置多個目標的 build.zig 設定。另附一份關於目標三元組與嵌入式連結腳本設定的參考文件。
適用情境
適用於將 Zig 程式交叉編譯到其他作業系統或架構(例如 ARM、Windows 或 macOS)時。也適用於關於 Zig 目標三元組、以 zig cc 交叉編譯 C 程式碼、嵌入式裸機目標或 WebAssembly 輸出的問題。
執行需求
需要 Zig 工具鏈;部分指令使用 python3 解析 zig targets 輸出,WASI 與 WASM 體積最佳化可選用 wasmtime 與 wasm-opt 等工具。macOS 目標需要 macOS SDK。不附帶指令碼,僅包含說明與一份參考文件。

Zig Cross-Compilation

Purpose

Guide agents through Zig's built-in cross-compilation: target triple selection, CPU feature targeting, zig cc for cross-compiling C projects, embedded bare-metal targets, and WASM output — all without requiring a system cross-toolchain.

Triggers

  • "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?"

Workflow

1. Zig's native cross-compilation

Zig has cross-compilation built in — no cross-toolchain, no Docker, no sysroot needed for pure Zig code:

bash
# List all supported targetszig targets | python3 -c "import sys,json; d=json.load(sys.stdin); [print(t) for t in d['libc']]"
# Build for a specific targetzig build-exe src/main.zig -target aarch64-linux-gnu -O ReleaseFast
# With build system (pass target as option)zig build -Dtarget=aarch64-linux-gnu -Doptimize=ReleaseFast
# Cross-compile for Windows from Linux/macOSzig build-exe src/main.zig -target x86_64-windows-gnu
# Cross-compile for macOS from Linux (requires macOS SDK)zig build-exe src/main.zig -target aarch64-macos-none

2. Common target triples

Target triplePlatform
x86_64-linux-gnuLinux x86-64 (glibc)
x86_64-linux-muslLinux x86-64 (musl, static)
aarch64-linux-gnuARM64 Linux (Pi 4, AWS Graviton)
aarch64-linux-muslARM64 Linux static
armv7-linux-gnueabihfARM 32-bit Linux (Pi 2/3)
x86_64-windows-gnuWindows x86-64
aarch64-macos-nonemacOS Apple Silicon
x86_64-macos-nonemacOS Intel
wasm32-freestandingWASM (browser, no OS)
wasm32-wasiWASM with WASI
thumbv7m-freestanding-eabiCortex-M3 bare metal
thumbv7em-freestanding-eabihfCortex-M4/M7 with FPU
riscv32-freestandingRISC-V 32-bit bare metal

3. CPU feature targeting

bash
# Native CPU (auto-detect, only for native builds)zig build-exe src/main.zig -mcpu native
# Baseline for architecture (most compatible)zig build-exe src/main.zig -target x86_64-linux-gnu -mcpu baseline
# x86-64 with AVX2zig build-exe src/main.zig -target x86_64-linux-gnu -mcpu x86_64+avx2+bmi2
# Raspberry Pi 4 (Cortex-A72)zig build-exe src/main.zig -target aarch64-linux-gnu -mcpu cortex_a72
# Cortex-M4 with FPUzig build-exe src/main.zig \    -target thumbv7em-freestanding-eabihf \    -mcpu cortex_m4+vfp4
# List CPU features for a targetzig targets | python3 -c "import sys,jsond=json.load(sys.stdin)for c in d['cpus']:    if 'cortex' in c['name']:        print(c['name'])"

4. zig cc for C cross-compilation

zig cc cross-compiles C without a system cross-toolchain:

bash
# Cross-compile C for ARM64 Linuxzig cc -target aarch64-linux-gnu -O2 -o myapp-arm64 main.c
# Cross-compile C for Windowszig cc -target x86_64-windows-gnu main.c -o myapp.exe
# Cross-compile C with static musl linkingzig cc -target x86_64-linux-musl -static main.c -o myapp-static
# Use in Makefile for all platformsCC_LINUX_AMD64 = zig cc -target x86_64-linux-gnuCC_LINUX_ARM64 = zig cc -target aarch64-linux-gnuCC_WINDOWS     = zig cc -target x86_64-windows-gnu

5. Build system cross-compilation

zig
// build.zig — cross-build all targetspub 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
# Build all targetszig build# Creates: zig-out/bin/myapp-x86_64, myapp-aarch64, myapp-x86_64.exe, myapp-aarch64

6. Embedded (bare-metal) targets

bash
# Cortex-M4 with FPU (STM32F4xx)zig build-exe src/main.zig \    -target thumbv7em-freestanding-eabihf \    -mcpu cortex_m4+vfp4 \    -O ReleaseSmall \    --script linker.ld
zig
// src/main.zig — bare metal entry pointconst 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

bash
# WASM freestanding (browser)zig build-exe src/main.zig \    -target wasm32-freestanding \    -O ReleaseSmall \    --export=init \    --export=update \    -fno-entry
# WASM WASI (wasmtime, wasmer)zig build-exe src/main.zig \    -target wasm32-wasi \    -O ReleaseSafewasmtime myapp.wasm
# Optimize WASM sizewasm-opt -Oz myapp.wasm -o myapp.opt.wasm

For target triple reference and embedded linker script setup, see references/zig-target-triples.md [blocked].

Related skills

  • Use skills/zig/zig-compiler for single-file compilation flags
  • Use skills/zig/zig-build-system for multi-target build.zig configuration
  • Use skills/compilers/cross-gcc for system cross-toolchain setup when needed
  • Use skills/rust/rust-cross for Rust's cross-compilation approach comparison

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/zig/zig-cross提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架