Zig’s build system compiles Zig, C, and C++ projects, with support for cross-compilation, custom build logic, and reproducible package management. Its build scripts are Zig programs that describe how build steps fit together. section 1.8 section 1.2.1
A minimal application build script creates a module from src/main.zig, adds an executable, and installs the artifact. Standard target and optimization options let one build script serve different environments and use cases; running zig build builds the installation step. section 3.5.2
Listing 3.12 build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); #1
const optimize = b.standardOptimizeOption(.{}); #1
const mod = b.createModule(.{ #2
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{ #3
.name = "cat",
.root_module = mod,
});
b.installArtifact(exe); #4
}
A complete minimal build.zig showing target and optimization options, module creation, executable creation, and artifact installation. section 3.5.2
Table 3.1 Optimize modes
| Name | Features | Purpose |
Debug |
Fast compilation speed. Compatible with debuggers. Runtime safety features on, and no optimizations enabled. |
Tailored to quickly iterating on code and diagnosing issues. |
ReleaseFast |
No runtime safety. Optimize for execution speed. |
Makes your code zoom! |
ReleaseSmall |
No runtime safety. Optimize for binary size. |
Makes your code tiny, which is often helpful for embedding. |
ReleaseSafe |
Runtime safety on. Limited optimization for speed and size. |
Perfect for when avoiding unexpected behavior is the priority. |
The four optimization modes balance compile speed, runtime safety, execution speed, and binary size. section 3.5.2
