These exercises use the material from Chapters 15.1 through 15.8. Most are small. The goal is to practice reading and writing build.zig files until the structure becomes familiar.
These exercises use the material from Chapters 15.1 through 15.8. Most are small. The goal is to practice reading and writing build.zig files until the structure becomes familiar.
Exercise 15-33
Create this project layout:
project/
├── build.zig
├── build.zig.zon
├── src/
│ └── main.zig
└── examples/
└── hello.zigBuild the executable with:
zig buildRun the example with:
zig build run-exampleExercise 15-34
Add these build steps:
check
run
test
fmt
examplesVerify that:
zig build --helpshows all five.
Exercise 15-35
Write a program that accepts command-line arguments.
Pass arguments through the build system:
zig build run -- alpha beta gammaPrint each argument on a separate line.
Exercise 15-36
Add a build option:
-Dtrace=trueExpose the value through a generated configuration module named config.
Print:
trace enabledonly when the option is true.
Exercise 15-37
Add a second build option:
-Dport=9000Use it as the default port number in the program.
Exercise 15-38
Write tests for these functions:
max
min
clampUse:
std.testing.expectEqualinstead of expect where possible.
Exercise 15-39
Write a test that intentionally leaks memory.
Observe the failure from the testing allocator.
Then fix the leak.
Exercise 15-40
Create two examples:
examples/read-file.zig
examples/write-file.zigAdd one examples build step that builds both.
Exercise 15-41
Add a dependency to build.zig.zon.
Import it into the main executable.
Then create a second executable that does not receive the dependency.
Verify that the second executable cannot import the module.
Exercise 15-42
Build the project for:
x86_64-linux
x86_64-windows
aarch64-macosObserve the output directory structure.
Exercise 15-43
Write a small platform abstraction:
pub fn separator() u8Return '\\' on Windows and '/' elsewhere.
Hide the implementation behind a module import.
Exercise 15-44
Add a ci step that depends on:
check
test
fmt
examplesRun:
zig build ciExercise 15-45
Write a helper function in build.zig:
fn addExample(...)Use it to reduce duplicated code when adding example executables.
Exercise 15-46
Create a static library:
const lib = b.addStaticLibrary(...)Link it into an executable.
Exercise 15-47
Create a shared library.
Build it for Linux and Windows.
Observe the generated file extensions.
Exercise 15-48
Add a generated source file step.
Generate a Zig source file containing:
pub const version = "0.1.0";Import the generated module into the main program.
Exercise 15-49
Write a program that prints:
architecture
operating system
pointer width
endiannessusing compile-time target information.
Build it for several targets.
Exercise 15-50
Take a small command-line program from an earlier chapter and reorganize it as a complete Zig project with:
build.zigbuild.zig.zon- tests
- examples
- custom build steps
- build options
- cross-build support
The final project should build cleanly with:
zig build
zig build test
zig build run
zig build examples
zig build ci