Skip to content

Lesson 5 of 9 · 24m

Cross-Compilation and the Zig Build System

Model targets explicitly, use zig build as the project contract, produce static binaries where appropriate, and understand what cross-compilation does not solve.

Zig's cross-compilation story feels like cheating the first time you use it. Then you ship a binary that crashes on an older glibc, links a system library you forgot to account for, or behaves differently because target CPU features changed. Cross-compilation is powerful precisely because it is explicit: target, ABI, CPU model, optimization mode, and linkage are build inputs. Treat them as release engineering, not as a lucky one-liner.

zig build is the project contract

A minimal build.zig describes the artifact and its target:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "spray",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    b.installArtifact(exe);
}

Now the interface is stable:

zig build
zig build -Doptimize=ReleaseSmall
zig build -Dtarget=x86_64-linux-musl -Doptimize=ReleaseSmall

The exact mode names matter less than the workflow: contributors should not need to remember compiler flags. Encode them in build steps.

Understand the target triple

Targets are architecture, operating system, and ABI:

x86_64-linux-gnu       glibc Linux on 64-bit x86
x86_64-linux-musl      musl Linux on 64-bit x86
aarch64-linux-musl     musl Linux on ARM64
x86_64-windows-gnu     Windows GNU ABI
aarch64-macos          macOS on Apple silicon
wasm32-freestanding    bare WebAssembly, no host OS assumptions

Musl static binaries are often the least surprising deployment artifact for Linux utilities. They are not automatically correct for every workload: NSS modules, some locale behavior, dlopen-dependent plugins, and libc-specific edge cases can change.

CPU features are part of correctness

Zig can target baseline or specific CPU features. Baseline maximizes compatibility; native maximizes use of the machine you compile on. Do not accidentally ship native binaries to unknown users:

zig build -Dtarget=x86_64-linux-musl -Dcpu=baseline

For services under your control, a specific CPU target may be reasonable. For downloadable tools, baseline or a clearly named microarchitecture level is safer. Illegal instruction crashes are not a performance strategy.

Cross-compiling is not cross-testing

A compiler can produce an ARM64 Linux binary on an x86_64 workstation. It cannot tell you whether the binary behaves correctly under the target kernel, filesystem, seccomp policy, or container runtime. Use QEMU, remote machines, or CI runners for smoke tests:

zig build -Dtarget=aarch64-linux-musl -Doptimize=ReleaseSmall
file zig-out/bin/spray
qemu-aarch64-static zig-out/bin/spray --help

At minimum verify startup, argument parsing, filesystem behavior, networking assumptions, and exit codes. Endianness and alignment bugs especially love targets you never execute.

Build artifacts need provenance

Release scripts should record compiler version, target, optimization mode, commit, and checksum:

zig build -Dtarget=x86_64-linux-musl -Doptimize=ReleaseSmall
sha256sum zig-out/bin/spray > spray-x86_64-linux-musl.sha256

This is not ceremony. When a user reports a crash, you need to know exactly which artifact they have.

Field notes

  • Linking libc narrows portability unless you choose musl/static linkage or control the deployment glibc version.
  • macOS cross-compilation may involve SDK and signing constraints that a Linux host cannot magically solve.
  • zig build test should be part of the same build contract as executables, not a separate incantation only maintainers know.
  • Cross-compiling C dependencies can fail even when pure Zig succeeds; inspect every external library's target assumptions.
  • Smaller is not automatically better. Compare ReleaseSafe, ReleaseSmall, and ReleaseFast by measuring startup, memory, throughput, and crash behavior.
  • Cache paths and absolute paths embedded at build time become reproducibility problems. Keep generated artifacts relocatable when possible.

Was this lesson helpful?

Sign in to rate