Build Mode

Zig has four build modes:

To add standard build options to a build.zig file:

build.zig

  1. const std = @import("std");
  2. pub fn build(b: *std.Build) void {
  3. const optimize = b.standardOptimizeOption(.{});
  4. const exe = b.addExecutable(.{
  5. .name = "example",
  6. .root_source_file = .{ .path = "example.zig" },
  7. .optimize = optimize,
  8. });
  9. b.default_step.dependOn(&exe.step);
  10. }

This causes these options to be available:

-Doptimize=Debug

Optimizations off and safety on (default)

-Doptimize=ReleaseSafe

Optimizations on and safety on

-Doptimize=ReleaseFast

Optimizations on and safety off

-Doptimize=ReleaseSmall

Size optimizations on and safety off

Debug

Shell

  1. $ zig build-exe example.zig
  • Fast compilation speed
  • Safety checks enabled
  • Slow runtime performance
  • Large binary size
  • No reproducible build requirement

ReleaseFast

Shell

  1. $ zig build-exe example.zig -O ReleaseFast
  • Fast runtime performance
  • Safety checks disabled
  • Slow compilation speed
  • Large binary size
  • Reproducible build

ReleaseSafe

Shell

  1. $ zig build-exe example.zig -O ReleaseSafe
  • Medium runtime performance
  • Safety checks enabled
  • Slow compilation speed
  • Large binary size
  • Reproducible build

ReleaseSmall

Shell

  1. $ zig build-exe example.zig -O ReleaseSmall
  • Medium runtime performance
  • Safety checks disabled
  • Slow compilation speed
  • Small binary size
  • Reproducible build

See also: