0Pricing
Zig Academy · Lesson

Build Options and Flags

Configure builds from the command line.

Build Options and Flags is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Configure from the CLI

A good build adapts without code edits. Zig lets you expose options that users set on the command line at build time. ⚙️

The Optimize Flag

Because you used standardOptimizeOption, users pass -Doptimize to pick a mode, like -Doptimize=ReleaseFast for a fast build.

zig build -Doptimize=ReleaseFast

The Target Flag

Likewise standardTargetOptions reads -Dtarget, so you can build for another platform from your own machine in one command.

zig build -Dtarget=x86_64-linux

Define Your Own Option

Add a custom flag with b.option. You give the type, the flag name, and a help description for users.

const verbose = b.option(bool, "verbose", "Log extra output");

Options Are Optional

b.option returns an optional: null when the user did not pass it. Use orelse to supply a sensible default value.

const v = verbose orelse false;

Pass It on the CLI

The user sets a boolean option by name. So -Dverbose=true turns it on, and leaving it off keeps your default.

zig build -Dverbose=true

Many Value Types

Options are not just booleans. You can declare integers, strings, or enums, and Zig parses and validates the value for you.

const n = b.option(u32, "threads", "Worker count");

Send Options into Code

To reach your program, build an options module with b.addOptions and add fields your source can read at compile time.

const opts = b.addOptions();

Add a Compile-Time Constant

Place a value into that module with addOption, naming the constant your code will import and read.

opts.addOption(bool, "verbose", v);

Import Options in Source

Expose the module to an artifact with addOptions, then in your code import it by name to read those constants.

const config = @import("config");

Discover Every Flag

Every option you declare shows up in zig build --help, complete with its description, so users learn how to configure your build. ✅

zig build --help

Quick Check

Recall the command-line shape of a custom build option.

Recap

You configured builds with flags: built-in -Doptimize and -Dtarget, custom b.option values, and addOptions to pass constants into your code. 🎯

Frequently asked questions

Is the “Build Options and Flags” lesson free?

Yes — the full text of “Build Options and Flags” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Build Options and Flags”?

Configure builds from the command line. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Build Options and Flags” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Anatomy of a build.zig File
  2. Declaring Executables and Libraries
  3. Build Steps and zig build run
  4. Build Options and Flags
← Back to Zig Academy