Zig compiler
Contract
| Field | Bound contract |
|---|---|
| Trigger | The user compiles a Zig file from the command line, asks which optimize mode to use, wants zig cc as a C or C++ compiler, needs assembly or LLVM IR from Zig, or shows a Zig compile error. |
| Authority | Read-only. The skill emits commands and readings to chat; the user runs them. Rollback is not needed. No remote mutation. |
| Side effect | Chat output; scratch compiles write into a scratch directory. |
| Done | The command for the request is reported, every flag in it is confirmed against zig build-exe --help on the installed Zig, and each error message in the request has a reading and fix. |
Inputs
- Zig version from
zig version: required. Everything below was run on Zig 0.14.1; flag spellings change between releases. - Source file or C source: required.
- Goal: required. Debug build, safe release, fastest, smallest, library, C compile, inspection, or error reading.
- Target when not the host: optional; see zig-cross for the triple and CPU choices.
Procedure
Compile. Run in place:
zig run src/main.zig. Executable:zig build-exe src/main.zig, named output-femit-bin=myapp. Static library:zig build-lib src/mylib.zig; shared:zig build-lib src/mylib.zig -dynamic. Object:zig build-obj src/main.zig. Link libc when C is involved:-lc. Done when: the artifact exists.Choose the optimize mode with
-O <mode>. Done when: the mode matches the goal.Mode Safety checks Use Debug(default)On, with debug info Development ReleaseSafeOn Production where a panic beats undefined behavior ReleaseFastOff Throughput, only after the code passed under ReleaseSafeReleaseSmallOff Size-constrained targets: embedded, WebAssembly With checks on, integer overflow, out-of-bounds indexing, unwrapping
null, reachingunreachable, and an invalid enum tag all panic with a source location; with checks off the same conditions are undefined behavior. Safe alternatives when the arithmetic is intentional:+%wraps,@addWithOverflowreports the overflow bit,std.math.addreturns an error. A comptime overflow is a compile error in every mode. Done when: the user knows what each mode does on error.Use
zig ccandzig c++as a C and C++ compiler. They accept Clang flags:zig cc -O2 -Wall main.c -o myapp,zig c++ -std=c++17 -O2 main.cpp -o myapp. Cross-compile without a system toolchain:zig cc -target aarch64-linux-gnu -O2 main.c -o myapp-arm; static musl:zig cc -target x86_64-linux-musl -static main.c -o myapp-static. In CMake:CC="zig cc" CXX="zig c++" cmake -S . -B build; in Make:CC="zig cc" make. Zig bundles the libc it links for the triples listed under.libcinzig targets; no system cross toolchain is needed. Done when: the C artifact runs on its target.Emit intermediate forms with
-femit-asm=<path>and-femit-llvm-ir=<path>(also-femit-llvm-bc); add-fno-emit-binto skip the binary. There are no--emit-asmor--emit-llvm-irspellings. Done when: the requested file exists.Validate without compiling:
zig ast-check src/main.zigchecks syntax and AST-level errors;zig fmt src/formats a tree,zig fmt --check src/reports files that would change. Done when: the check passes or the reported file is named.Read compile errors: each has
file:line:col: error: <message>, a caret under the token, andnote:lines for context. Done when: each message maps to a fix.Message Meaning and fix expected type 'X', found 'Y'Type mismatch; convert explicitly ( @intCast,@as) or fix the declarationuse of undeclared identifier 'X'Missing import or typo type 'u8' cannot represent integer value '300'Comptime overflow; widen the type cannot assign to constantconstbinding mutated; make itvarunused local constantorunused local variableEvery local must be used; discard with _ = x;unused function parameter 'x'Same rule; _ = x;in the bodyRead runtime panics such as
thread <id> panic: integer overfloworpanic: index out of bounds: index 5, len 3followed by a source location: the check fired inDebugorReleaseSafe; fix the arithmetic or bound. Deeper debugging: use zig-debugging. Done when: the panic maps to the line it names.Confirm environment facts with
zig versionandzig env(which printszig_exeandlib_dir). Done when: the version and library directory are known.
Failure and recovery
| Failure class | Behavior |
|---|---|
Flag rejected as unrecognized parameter |
Look it up in zig build-exe --help; only -f-prefixed emit flags exist. |
Unsupported operating system freestanding from std |
The program touches std facilities that need an OS on a freestanding target; restructure or pick a hosted target. |
zig cc link fails against a system library |
Pass -l<name> and, for a non-native target, the library must exist for that target; there is no host library reuse. |
| Multi-file or multi-target project | Move to build.zig: use zig-build-system. |
| Target triple or CPU question | Use zig-cross. |
Output
A chat report with the compile command, the optimize mode and its safety consequence, any emit or check commands, and a reading of each error or panic in the request, each confirmed on the installed Zig.