Project
zig init
Scaffold build.zig, build.zig.zon and src/
zig init --minimal
Only write a stub build.zig and .zon
zig version
Print the compiler version
zig env
Show lib, std and cache directories
zig fetch --save <url>
Add a dependency to build.zig.zon
zig fetch --save=<name> <url>
Add it under a chosen name
zig build --fetch
Download all dependencies and exit
Build system
zig build
Run the default install step
zig build run
Build and run the executable
zig build run -- <args>
Pass arguments to the program
Everything after -- goes to your program, not to the build
system.
zig build test
Run the project's test step
zig build -l
List the available steps
zig build --help
Show all options, including project -D ones
zig build <step>
Run a custom step from build.zig
zig build --summary all
Show every step, not just failures
zig build -p <dir>
Install into <dir> instead of zig-out
zig build --watch
Rebuild when sources change
zig build -j<N>
Limit concurrent jobs
zig build -D<name>=<value>
Set a project option
zig build -fincremental
Use incremental compilation
Optimize and target
zig build -Doptimize=Debug
Default: safety checks on, no optimization
zig build -Doptimize=ReleaseSafe
Optimized, keeps safety checks
zig build -Doptimize=ReleaseFast
Fastest code, safety checks off
zig build -Doptimize=ReleaseSmall
Smallest binary
zig build --release=fast
Pick a release mode (fast, safe, small)
zig build -Dtarget=<triple>
Cross compile the whole project
zig build -Dtarget=x86_64-windows
Example: Windows 64-bit
zig build -Dcpu=<features>
Tune CPU features
zig targets
List supported targets and CPUs
Single files
zig run <file.zig>
Compile and run a file
zig run <file.zig> -- <args>
Run with program arguments
zig build-exe <file.zig>
Compile an executable
zig build-lib <file.zig>
Compile a static library
zig build-obj <file.zig>
Compile an object file
zig build-exe <file.zig> -O ReleaseFast
Set the optimization mode
zig build-exe <file.zig> -target <triple>
Cross compile one file
zig build-exe <file.zig>
-femit-bin=<path>
Choose the output path
zig build-exe <file.zig> -lc
Link libc
zig build-exe <file.zig> -l<lib>
Link a system library
zig build-exe <file.zig> -I<dir>
Add an include directory
zig build-exe <file.zig> -fstrip
Strip debug info
zig cc <file.c> -o <out>
Compile C with the bundled clang
zig c++ <file.cpp> -o <out>
Compile C++
zig cc -target <triple> <file.c>
Cross compile C
zig cc -target x86_64-linux-musl <file.c>
Example: static Linux binary
zig translate-c <file.h>
Convert C to Zig on stdout
zig ar rcs <lib.a> <objs>
Make a static archive
zig objcopy <in> <out>
Copy or strip executables and objects
CC="zig cc" make
Use zig as the C compiler in a make build