Zig Cross-Compilation
Cross-compiling means building a program on one kind of computer for another kind: a Windows .exe on a Linux laptop, or a Raspberry Pi program on a Mac. In most languages that’s a chore. You install a separate toolchain for each target, find the right C library, set up a “sysroot” and fight with linker flags. With Zig it’s usually one flag, -target, because the Zig download already contains what it needs to build for dozens of systems. This page shows how that works, what it can and can’t do, and how to build a release for several systems at once.
Everything here was checked with Zig 0.17.1, building on Linux x86-64. Cross-compiling works the same from macOS and Windows.
Your first cross-compile
Section titled “Your first cross-compile”Here’s a program that says what it was built for:
const std = @import("std");const builtin = @import("builtin");
pub fn main() void { std.debug.print("Hello from {t} on {t}!\n", .{ builtin.target.os.tag, builtin.target.cpu.arch });}Output:
Hello from linux on x86_64!That’s a normal build for the machine you’re on. Now build the same file for Windows:
zig build-exe hello.zig -target x86_64-windowsYou get hello.exe (and hello.pdb, the Windows debug information file). The file command, which is on most Linux and macOS systems, confirms what it is:
file hello.exeYou’ll see:
hello.exe: PE32+ executable (console) x86-64, for MS Windows, 7 sectionsThat’s a real Windows program. Copy it to a Windows PC and it prints Hello from windows on x86_64!. Now a 64-bit ARM Linux build, the kind that runs on a Raspberry Pi 4 or 5 with a 64-bit OS, or on an ARM server:
zig build-exe hello.zig -target aarch64-linuxfile helloYou’ll see:
hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not strippedNothing to install, nothing to configure. If you try to run that file on an x86-64 PC, the system refuses, because it’s machine code for a different CPU:
bash: ./hello: cannot execute binary file: Exec format errorThat’s expected. Copy it to an ARM machine to run it, or use an emulator such as QEMU (more on that below).
Reading a target
Section titled “Reading a target”A target has up to three parts, joined with dashes: CPU architecture, operating system and ABI (roughly, “which conventions and C library”).
| Target | Means |
|---|---|
x86_64-windows |
64-bit Intel/AMD Windows |
aarch64-macos |
Apple Silicon Mac (M1 and later) |
x86_64-macos |
Intel Mac |
aarch64-linux |
64-bit ARM Linux (Raspberry Pi 4/5 on a 64-bit OS, ARM servers) |
arm-linux-musleabihf |
32-bit ARM Linux with hardware floats (older Raspberry Pi OS) |
x86_64-linux-gnu |
64-bit Linux using glibc, the usual C library on Ubuntu, Debian, Fedora |
x86_64-linux-musl |
64-bit Linux using musl, good for fully static programs (Alpine uses it) |
riscv64-linux |
64-bit RISC-V Linux |
wasm32-wasi |
WebAssembly with WASI, to run in a runtime like wasmtime |
If you leave the ABI out, Zig picks a sensible one. For Linux that’s musl, so x86_64-linux gives you a static program with no outside dependencies.
All of these built fine here, from the same hello.zig. Here’s what file says about a few of them (shortened):
aarch64-macos: Mach-O 64-bit arm64 executableriscv64-linux: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linkedwasm32-wasi: WebAssembly (wasm) binary module version 0x1 (MVP)For wasm32-wasi, Zig writes hello.wasm. A WASI runtime such as wasmtime runs it with wasmtime hello.wasm. We ran it through Node.js’s built-in WASI support and got Hello from wasi on wasm32!.
zig targets
Section titled “zig targets”zig targets prints everything this Zig can build for. It’s long (over 35,000 lines, most of it CPU details), written in Zig’s data syntax. The start looks like this:
zig targetsYou’ll see something like (trimmed):
.{ .arch = .{ "aarch64", "aarch64_be", ... }, .os = .{ "freestanding", ... }, .libc = .{ "aarch64-linux-gnu", "aarch64-linux-musl", "aarch64-macos-none", ... },The .libc list is the useful one: those are the targets for which Zig can provide a C library itself (108 of them in 0.17.1). The .native section at the very end shows what Zig thinks your own machine is.
The CPU matters too: native vs baseline
Section titled “The CPU matters too: native vs baseline”Here’s a trap that catches people shipping Linux binaries. When you build without -target, Zig builds for your exact CPU, using every instruction it has. On the machine used for this page, that meant AVX-512 instructions. Copy that program to an older x86-64 PC and it can crash with “illegal instruction”.
When you pass a -target, Zig assumes a baseline CPU instead: one that every machine of that architecture can run. So for a program you’ll hand to other people, name the target, even if it’s the same as yours:
zig build-exe hello.zig -target x86_64-linux -O safe # runs on any x86-64 LinuxYou can also set the CPU directly with -mcpu: -mcpu=baseline for the safe choice, or a specific level like -mcpu=x86_64_v3 if you know all your users have CPUs from roughly the last ten years and want the newer instructions.
When libc comes in
Section titled “When libc comes in”A program written only in Zig doesn’t need a C library at all. Zig talks to the operating system directly, so the Linux builds above are statically linked: one file, no dependencies. That’s why they’re so easy to move around.
You need libc when your program calls C code: a C library you link, std.heap.c_allocator, or anything extern "c". You ask for it with -lc. Here’s a program that allocates through libc’s malloc:
const std = @import("std");
pub fn main() void { const gpa = std.heap.c_allocator; // malloc and free from libc const buf = gpa.alloc(u8, 16) catch return; defer gpa.free(buf); std.debug.print("got {d} bytes from libc\n", .{buf.len});}Forget -lc and Zig tells you:
error: dependency on libc must be explicitly specified in the build commandWith -lc, it cross-compiles to all the main systems, because Zig ships the C library pieces for them:
zig build-exe withc.zig -lc -target x86_64-linux-musl # static, no dependencieszig build-exe withc.zig -lc -target x86_64-linux-gnu # uses the system's glibczig build-exe withc.zig -lc -target x86_64-windows # uses MinGW-w64's C libraryzig build-exe withc.zig -lc -target aarch64-macos # uses macOS's libSystemAll four built here. A few things to know:
- glibc version. With
-gnu, the program needs a glibc at least as new as the one you built against. Zig 0.17.1 picks glibc 2.31 by default, and you can choose an older one to support older Linux systems:-target x86_64-linux-gnu.2.28. - musl gives you a fully static program that runs on any Linux of that CPU type. It’s the easy choice for command-line tools.
- The first build for a new target can be slow. Zig builds that target’s C library from source the first time and caches it. Compiling a C file for Windows the first time took over two minutes here; the next build took under a second.
What still needs the real system: SDKs and system libraries
Section titled “What still needs the real system: SDKs and system libraries”Zig brings a C library, but it doesn’t bring every library on every system. Two things need files you have to provide:
-
macOS frameworks like Cocoa, Metal or CoreFoundation. They’re part of Apple’s SDK, which Zig can’t ship. Asking for one without the SDK fails:
error: unable to find system framework "CoreFoundation". searched paths: nonePlain Zig programs (and programs that use libc only) cross-compile to macOS fine, as shown above. Apps with a GUI or other Apple frameworks are usually built on a Mac, or with a copy of the SDK passed through
--sysroot. -
Third-party system libraries such as OpenSSL, SQLite or GTK, when you link the copy installed on the target system. Zig doesn’t have them for other systems:
error: unable to find dynamic system library "ssl" using strategy paths_first. searched paths: noneThe usual fix is to build the library from source as part of your build (many C libraries are available as Zig packages that do exactly that, see Zig Packages & Dependencies), or to point Zig at a copy of the target’s files with
--sysroot.
Cross-compiling with zig build
Section titled “Cross-compiling with zig build”In a project, use -Dtarget (the template’s standardTargetOptions provides it):
zig build -Dtarget=x86_64-windows -Doptimize=smallThe program lands in zig-out/bin/ as usual, here as greeter.exe. But zig build run can’t run a program built for another system:
error: unable to spawn foreign binary './zig-out/bin/greeter'If you have an emulator installed, zig build can use it: -fqemu runs other-CPU Linux programs with QEMU, -fwine runs Windows programs with Wine, and -fwasmtime runs WASI programs. zig build --help lists them under “System Integration Options”.
One command, many targets
Section titled “One command, many targets”For a release you often want the same program for several systems. Since build.zig is just Zig, a loop does it. This is adapted from the release example in the official build system guide:
const std = @import("std");
const targets: []const std.Target.Query = &.{ .{ .cpu_arch = .x86_64, .os_tag = .linux }, .{ .cpu_arch = .aarch64, .os_tag = .linux }, .{ .cpu_arch = .x86_64, .os_tag = .windows }, .{ .cpu_arch = .aarch64, .os_tag = .macos },};
pub fn build(b: *std.Build) !void { for (targets) |query| { const exe = b.addExecutable(.{ .name = "hello", .root_module = b.createModule(.{ .root_source_file = b.path("hello.zig"), .target = b.resolveTargetQuery(query), .optimize = .small, }), }); const install = b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = try query.zigTriple(b.allocator) } }, }); b.getInstallStep().dependOn(&install.step); }}std.Target.Query describes a target in code, b.resolveTargetQuery turns it into a real target, and .dest_dir puts each build in its own folder named after the target. Note that build returns !void here, so it can use try.
zig build --summary allYou’ll see something like:
Build Summary: 9/9 steps succeededinstall success+- install hello success| +- compile exe hello small x86_64-linux success 2s MaxRSS:167M+- install hello success| +- compile exe hello small aarch64-linux success 6s MaxRSS:268M+- install hello success| +- compile exe hello small x86_64-windows success 2s MaxRSS:173M+- install hello success +- compile exe hello small aarch64-macos success 2s MaxRSS:201Mand four programs, built in parallel (about 7 seconds in total here):
zig-out/├── aarch64-linux/hello├── aarch64-macos/hello├── x86_64-linux/hello└── x86_64-windows/hello.exeCross-compiling C, too
Section titled “Cross-compiling C, too”Everything above also works for C and C++ through zig cc and zig c++, which take the same -target:
zig cc -target aarch64-linux-musl hi.c -o hi-armfile hi-armYou’ll see:
hi-arm: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not strippedThat’s why some teams that never write Zig use the Zig download as their C cross-compiler. Zig as a C Compiler covers it.
Practice
Section titled “Practice”1. Build for a Raspberry Pi
Section titled “1. Build for a Raspberry Pi”Build the hello.zig from the top of this page for a Raspberry Pi running 64-bit Raspberry Pi OS, in small mode, and check the result with file.
Hint: 64-bit ARM is aarch64. Add -O small for a small file.
Show solution
zig build-exe hello.zig -target aarch64-linux -O smallfile helloYou’ll see:
hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, strippedCopy hello to the Pi (with scp, for example) and run it there: it prints Hello from linux on aarch64!. Because it’s static, it doesn’t care which Linux distribution the Pi runs.
2. Pick the right target
Section titled “2. Pick the right target”A friend runs an old Ubuntu server on an Intel CPU from 2012. You want to send them a command-line tool written in Zig that also links libc. Which zig build-exe command gives them a program that will run?
Hint: think about the CPU (native or baseline?) and the C library (glibc version, or none to worry about?).
Show solution
zig build-exe tool.zig -lc -target x86_64-linux-musl -O safeNaming the target gives a baseline x86-64 CPU, so no newer instructions sneak in. musl links the C library statically, so the program doesn’t depend on the server’s glibc version. If you need glibc, pick an old enough one instead, such as -target x86_64-linux-gnu.2.28.
3. A two-target release
Section titled “3. A two-target release”Change the multi-target build.zig above so it builds only x86_64-windows and x86_64-linux, in safe mode.
Hint: edit the targets list and the .optimize field.
Show solution
const targets: []const std.Target.Query = &.{ .{ .cpu_arch = .x86_64, .os_tag = .windows }, .{ .cpu_arch = .x86_64, .os_tag = .linux },};and in createModule, .optimize = .safe. zig build then creates zig-out/x86_64-windows/hello.exe and zig-out/x86_64-linux/hello. Expect it to take longer than the small build: safe builds are slower to compile.
-target arch-os-abibuilds for another system:x86_64-windows,aarch64-linux,aarch64-macos,wasm32-wasiand many more.zig targetslists them.- Pure Zig programs need no libc and come out static on Linux, so they’re easy to ship.
- Without
-target, Zig builds for your exact CPU. Name the target (or use-mcpu=baseline) for programs others will run. - With
-lc, Zig provides the C library for the main systems: glibc (choose a version like.2.28), musl (static), MinGW on Windows, libSystem on macOS. - macOS frameworks and third-party system libraries aren’t included: they need an SDK, a
--sysroot, or building the library from source. - In projects use
-Dtarget; abuild.zigloop can build a release for many targets at once.
Next up: Zig Main & Init, everything main can receive in Zig 0.17, from init.io to init.gpa.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Zig language reference: Targets: what a target is and how Zig names them.
- Zig 0.17.0 release notes: Target Support: the tier system, the full support table and minimum OS versions.
- Zig build system guide: the official multi-target release example and
-Dtarget. - Zig in-depth overview: cross-compiling: why cross-compiling is a first-class use case in Zig.