std.Build API provides the build system functionality used in build.zig files. This is the primary interface for configuring how Zig projects are compiled.
Overview
Everybuild.zig file exports a build function that receives a *Build parameter:
Build Object
TheBuild type is the main entry point for build system operations.
Key Fields
Allocator for build-time allocations. Uses arena allocation.
Installation prefix path (e.g., “/usr” or “zig-out”).
Path to the directory containing build.zig.
Path to the cache directory.
Shared state among all Build instances.
Creating Compilation Artifacts
Executables
Creates a step that builds an executable.ExecutableOptions:Example:
Libraries
Creates a step that builds a library (static or dynamic).LibraryOptions:Example:
Object Files
Creates a step that builds an object file (.o).Example:
Tests
Creates an executable containing unit tests.Equivalent to
zig test --test-no-exec. This step does not run the tests; use addRunArtifact to create a run step.TestOptions:Example:Running Programs
Creates a step to run a compiled artifact.Example:
Creates a step to run a system command.Be careful using this function as it introduces a system dependency.Example:
Modules
Creates a module and adds it to the package’s module set, making it available to other packages which depend on this one.Example:
Creates a private module for use by the current package only.Use
addModule to create a public module.Example:Installation
Installs an artifact using default options. Adds the install step to the top-level install step dependencies.Example:
Creates an install step for an artifact with custom options. Does not automatically add to top-level install step.Example:
Installs a file to the installation prefix.Parameters:
source- Source file pathdest_rel_path- Destination path relative to prefix
Installs a directory.Example:
File Operations
Creates a step that writes files to the cache directory.Example:
Creates a step that updates source files (useful for generated code).Example:
Creates a C header file, possibly based on a template (e.g., config.h.in).Example:
Build Options
Creates a set of key-value pairs that can be converted into a Zig source file and imported.Provides a way to expose build.zig values to Zig source code.Example:In your source code:
User Options
Creates a configuration option to be passed to the build script.Users can set these with Usage:
-D arguments when running zig build.Returns null when the option is left to default.Parameters:T- Type of the optionname- Option namedescription- Help text
bool,int,floatenum[]const u8(string)[]const []const u8(list of strings)LazyPathstd.zig.BuildId
zig build -Denable-feature=true -Dlog-level=debugExposes standard Usage:
zig build options for choosing optimization mode.Example:zig build --release=fast or zig build -Doptimize=ReleaseFastExposes standard Usage:
zig build options for choosing a target and resolves the target query.StandardTargetOptionsArgs:Example:zig build -Dtarget=x86_64-linuxSteps
Creates a new top-level step that users can invoke.Parameters:Usage:
name- Step name (used inzig build <name>)description- Help text
zig build docsReturns the top-level install step.Example:
Dependencies
Fetches a dependency and returns its Build instance.Parameters:
name- Dependency name (from build.zig.zon)args- Options to pass to the dependency’s build.zig
Utilities
References a file or directory relative to the source root.Example:
String formatting using allocator.Example:
Duplicates a string without handling out of memory.Example:
Joins path components.Example:
Related Types
Step
Represents a single build step.Location:
lib/std/Build/Step.zigModule
Represents a Zig module with configuration.Location:
lib/std/Build/Module.zigCache
Build cache management.Location:
lib/std/Build/Cache.zigSee Also
- std namespace - Standard library exports
- builtin namespace - Compiler built-ins
- Build System Guide - Complete guide to build.zig