Zig Build System

The Zig Build System provides a cross-platform, dependency-free way to declare the logic required to build a project. With this system, the logic to build a project is written in a build.zig file, using the Zig Build System API to declare and configure build artifacts and other tasks.

Some examples of tasks the build system can help with:

  • Creating build artifacts by executing the Zig compiler. This includes building Zig source code as well as C and C++ source code.
  • Capturing user-configured options and using those options to configure the build.
  • Surfacing build configuration as comptime values by providing a file that can be imported by Zig code.
  • Caching build artifacts to avoid unnecessarily repeating steps.
  • Executing build artifacts or system-installed tools.
  • Running tests and verifying the output of executing a build artifact matches the expected value.
  • Running zig fmt on a codebase or a subset of it.
  • Custom tasks.

To use the build system, run zig build —help to see a command-line usage help menu. This will include project-specific options that were declared in the build.zig script.

Building an Executable

This build.zig file is automatically generated by zig init-exe.

build_executable.zig

  1. const std = @import("std");
  2. pub fn build(b: *std.Build) void {
  3. // Standard target options allows the person running `zig build` to choose
  4. // what target to build for. Here we do not override the defaults, which
  5. // means any target is allowed, and the default is native. Other options
  6. // for restricting supported target set are available.
  7. const target = b.standardTargetOptions(.{});
  8. // Standard optimization options allow the person running `zig build` to select
  9. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
  10. // set a preferred release mode, allowing the user to decide how to optimize.
  11. const optimize = b.standardOptimizeOption(.{});
  12. const exe = b.addExecutable(.{
  13. .name = "example",
  14. .root_source_file = .{ .path = "src/main.zig" },
  15. .target = target,
  16. .optimize = optimize,
  17. });
  18. exe.install();
  19. const run_cmd = exe.run();
  20. run_cmd.step.dependOn(b.getInstallStep());
  21. if (b.args) |args| {
  22. run_cmd.addArgs(args);
  23. }
  24. const run_step = b.step("run", "Run the app");
  25. run_step.dependOn(&run_cmd.step);
  26. }

Building a Library

This build.zig file is automatically generated by zig init-lib.

build_library.zig

  1. const std = @import("std");
  2. pub fn build(b: *std.Build) void {
  3. const optimize = b.standardOptimizeOption(.{});
  4. const lib = b.addStaticLibrary(.{
  5. .name = "example",
  6. .root_source_file = .{ .path = "src/main.zig" },
  7. .optimize = optimize,
  8. });
  9. lib.install();
  10. const main_tests = b.addTest(.{
  11. .root_source_file = .{ .path = "src/main.zig" },
  12. .optimize = optimize,
  13. });
  14. const test_step = b.step("test", "Run library tests");
  15. test_step.dependOn(&main_tests.step);
  16. }

Compiling C Source Code

  1. lib.addCSourceFile("src/lib.c", &[_][]const u8{
  2. "-Wall",
  3. "-Wextra",
  4. "-Werror",
  5. });