Cross-compile C and Zig with Zig 0.17.1
Select the target explicitly with zig cc -target or a build script using standardTargetOptions. Verify the file format separately from whether the artifact runs on the target.
Use zig cc -target for a C compilation and zig build -Dtarget= for a Zig build script that exposes the standard target option. The target selects architecture, operating system and ABI; it does not make every dependency portable. Check the resulting file format, then test it on the target system or an appropriate emulator.
Use Zig 0.17.1 for the commands below. Set ZIG to your own executable; the examples assume it is available as zig:
ZIG=zig
"$ZIG" version
Output:
0.17.1
Use Zig as the C compiler
Save this ordinary libc example as hello.c:
#include <stdio.h>
int main(void) { puts("hello"); return 0; }
"$ZIG" cc -target aarch64-linux-musl hello.c -o hello-arm64
"$ZIG" cc -target x86_64-windows-gnu hello.c -o hello.exe
"$ZIG" cc -target aarch64-macos hello.c -o hello-macos
file hello-arm64 hello.exe hello-macos
aarch64-linux-musl selects ARM64 Linux with musl; x86_64-windows-gnu selects the GNU Windows ABI rather than the MSVC toolchain. The Zig overview describes bundled C cross-compilation support. External C libraries still need target-compatible headers and libraries.
The macOS libc example can link without an Apple SDK:
hello-macos: Mach-O 64-bit arm64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|NO_REEXPORTED_DYLIBS|PIE>
That result covers this small libc call, not arbitrary Apple frameworks. An SDK-free libc build does not imply that Cocoa headers, third-party frameworks, code signing or deployment testing are provided. For object generation without libc, save int answer(void) { return 42; } as bare.c and run zig cc -target aarch64-macos -c bare.c -o bare.o to produce a Mach-O arm64 object. The 0.17.1 reference documents C compilation and target selection.
Check the compiler and linker separately
This deliberately minimal entry.c needs neither libc nor startup files:
void entry(void) { for (;;) {} }
"$ZIG" cc -target aarch64-linux-musl -nostdlib -Wl,-e,entry entry.c -o entry-arm64
"$ZIG" cc -target x86_64-windows-gnu -nostdlib -Wl,-e,entry entry.c -o entry.exe
file entry-arm64 entry.exe
Real output:
entry-arm64: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not stripped
entry.exe: PE32+ executable for MS Windows 6.00 (console), x86-64, 4 sections
file identifies the target architecture and executable format. Do not run them: the entry point intentionally loops forever. With -nostdlib, the Linux output contains no musl calls, so its format alone does not verify musl integration. -Wl, passes the explicit entry symbol to the linker.
Expose the target in build.zig
Save pub fn main() void {} as main.zig, then create:
const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "empty",
.root_module = b.createModule(.{
.root_source_file = b.path("main.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
}),
});
b.installArtifact(exe);
}
"$ZIG" build -j1 -Dtarget=aarch64-linux-musl -Doptimize=ReleaseSmall
file zig-out/bin/empty
Example output:
zig-out/bin/empty: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, stripped
standardTargetOptions makes -Dtarget meaningful; it is not automatically consumed by every custom build script. The executable uses a module with that target, and installArtifact places the result under the install prefix. -j1 limits build concurrency. The build-system guide documents these APIs. This empty program isolates build configuration from libc dependencies.
A correct file result establishes architecture and format, not runtime compatibility, minimum OS support or availability of dynamic libraries. Test the executable on its target operating system before distributing it.
Go deeper
See Zig compilation for what reaches the artifact and linking for the difference between an object and a usable executable.