# Exercises

### Exercises

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:

```text
project/
├── build.zig
├── build.zig.zon
├── src/
│   └── main.zig
└── examples/
    └── hello.zig
```

Build the executable with:

```sh
zig build
```

Run the example with:

```sh
zig build run-example
```

#### Exercise 15-34

Add these build steps:

```text
check
run
test
fmt
examples
```

Verify that:

```sh
zig build --help
```

shows all five.

#### Exercise 15-35

Write a program that accepts command-line arguments.

Pass arguments through the build system:

```sh
zig build run -- alpha beta gamma
```

Print each argument on a separate line.

#### Exercise 15-36

Add a build option:

```text
-Dtrace=true
```

Expose the value through a generated configuration module named `config`.

Print:

```text
trace enabled
```

only when the option is true.

#### Exercise 15-37

Add a second build option:

```text
-Dport=9000
```

Use it as the default port number in the program.

#### Exercise 15-38

Write tests for these functions:

```zig
max
min
clamp
```

Use:

```zig
std.testing.expectEqual
```

instead 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:

```text
examples/read-file.zig
examples/write-file.zig
```

Add 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:

```text
x86_64-linux
x86_64-windows
aarch64-macos
```

Observe the output directory structure.

#### Exercise 15-43

Write a small platform abstraction:

```zig
pub fn separator() u8
```

Return `'\\'` on Windows and `'/'` elsewhere.

Hide the implementation behind a module import.

#### Exercise 15-44

Add a `ci` step that depends on:

```text
check
test
fmt
examples
```

Run:

```sh
zig build ci
```

#### Exercise 15-45

Write a helper function in `build.zig`:

```zig
fn addExample(...)
```

Use it to reduce duplicated code when adding example executables.

#### Exercise 15-46

Create a static library:

```zig
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:

```zig
pub const version = "0.1.0";
```

Import the generated module into the main program.

#### Exercise 15-49

Write a program that prints:

```text
architecture
operating system
pointer width
endianness
```

using 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.zig`
- `build.zig.zon`
- tests
- examples
- custom build steps
- build options
- cross-build support

The final project should build cleanly with:

```sh
zig build
zig build test
zig build run
zig build examples
zig build ci
```

