liveBook · Manning

Zig build system

As explained in Systems Programming with Zig

The Zig build system is the build tool integrated with Zig for compiling Zig, C, and C++ projects and controlling build configuration.

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

How Systems Programming with Zig covers it

How deep it goes
Defines build scripts, modules, executables, installation, target options, and optimization modes through a practical application.
Examples
Builds a cat executable and demonstrates project layout, target selection, and four optimization modes.
What you can do after
Build from scratch by defining a module, executable, target, and optimization options, then installing the artifact with zig build.
How it connects
Connects build graphs to cross-compilation, C/C++ interoperability, comptime, and reproducible package management.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.