Zig Build System
C and C++ projects need extra tools — Make, CMake, Meson — each with its own language. Zig's build system is part of the compiler and uses Zig itself: a project is described by a file called build.zig, and zig build runs it. No extra installation, the same on every operating system, and fully capable of building C and C++ code as well.
Note: Build scripts can't run in the browser playground, so this lesson shows the files and simulated terminal output. Everything here matches the template that
zig initcreates in Zig 0.15.
The project layout
greeter/
├── build.zig the build script
├── build.zig.zon package name, version, dependencies
└── src/
├── main.zig executable entry point
└── root.zig library moduleA minimal build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
// command-line options: -Dtarget=... and -Doptimize=...
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "greeter",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
// `zig build` copies the executable to zig-out/bin
b.installArtifact(exe);
// `zig build run`
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// `zig build test`
const tests = b.addTest(.{ .root_module = exe.root_module });
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&b.addRunArtifact(tests).step);
}The build script doesn't do the work itself. It declares a graph of steps (compile, install, run, test) and their dependencies; zig build then executes the requested steps, in parallel where possible, and caches everything it can.
| Piece | Purpose |
|---|---|
b.standardTargetOptions |
lets users pick a target with -Dtarget= |
b.standardOptimizeOption |
lets users pick Debug/ReleaseSafe/… with -Doptimize= |
b.createModule |
a module: root file + target + optimisation |
b.addExecutable / b.addLibrary / b.addTest |
compile artifacts |
b.installArtifact |
copy the output into zig-out/ |
b.step("name", "help") |
a named step you can run with zig build name |
Running build steps
zig build --helpUsage: zig build [steps] [options]
Steps:
install (default) Copy build artifacts to prefix path
uninstall Remove build artifacts from prefix path
run Run the app
test Run unit tests
...
zig build run -- AliceHello, Alice!
Everything after -- is passed to your program as command-line arguments (that is what b.args handles).
Cross-compiling
Because Zig ships with the standard libraries for many platforms, building for another operating system or processor is one flag:
zig build -Dtarget=x86_64-windows -Doptimize=ReleaseSmall --prefix out-windows
zig build -Dtarget=aarch64-linux-musl -Doptimize=ReleaseFast --prefix out-arm
ls out-windows/bin out-arm/binout-arm/bin:
greeter
out-windows/bin:
greeter.exe greeter.pdb
--prefix chooses the install folder instead of zig-out, so the two builds don't overwrite each other. The Windows build also produces a .pdb file with debug information.
Dependencies: build.zig.zon
The .zon file (Zig Object Notation, a subset of Zig syntax) describes the package and lists dependencies with a URL and a content hash:
.{
.name = .greeter,
.version = "0.1.0",
.fingerprint = 0x9c5a8b1f2e3d4c6a, // generated by zig init, do not change
.minimum_zig_version = "0.15.2",
.dependencies = .{},
.paths = .{ "build.zig", "build.zig.zon", "src" },
}zig fetch --save <url> downloads a package, computes its hash and adds it to .dependencies. In build.zig you then import it:
const dep = b.dependency("some_lib", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("some_lib", dep.module("some_lib"));And in your code: const some_lib = @import("some_lib");.
Building C code
The same build script can compile C sources and link system libraries, which is why many C projects use Zig purely as a build tool:
exe.root_module.addCSourceFiles(.{
.files = &.{ "vendor/sqlite3.c" },
.flags = &.{ "-O2", "-DSQLITE_THREADSAFE=0" },
});
exe.root_module.addIncludePath(b.path("vendor"));
exe.root_module.link_libc = true;Useful commands
| Command | Effect |
|---|---|
zig build |
run the default install step |
zig build run |
build and run |
zig build test |
run unit tests |
zig build -Doptimize=ReleaseFast |
optimised build |
zig build --summary all |
show which steps ran or were cached |
zig build --watch |
rebuild automatically when files change |
Tip: The build API changes between Zig versions more than the language does. When copying a
build.zigfrom the internet, check which Zig version it was written for, and usezig initwith your own version as the reference.
Test yourself
In which language is a Zig build script written?
- CMake
- Make
- Zig
- YAML
What does zig build run -- x y do with x y?
- Ignores them
- Passes them to the program as arguments
- Treats them as build steps
- Uses them as targets
Which file lists a package's dependencies?
- build.zig.zon
- package.json
- deps.zig
- zig.lock