Skip to main content
skill CX Codex

zig-compiler — Claude Code skill: Zig builds and zig cc

Walks the agent through Zig compiler invocation: optimization modes, output types, zig cc as a C compiler, and reading error messages.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mohitmishra786/low-level-dev-skills --skill zig-compiler -a claude-code
Open on GitHub

What zig-compiler does

The skill fires on questions about zig build-exe, zig build-lib, the ReleaseSafe, ReleaseFast and ReleaseSmall modes, zig cc, or Zig compilation errors. It lays out the order of work: compiling a single file, picking an output type (executable, static or shared library, object file), cross-compiling by cpu-os-abi triple, emitting LLVM IR and assembly, and checking syntax with zig ast-check and zig fmt.

A separate section covers zig cc, the Clang-based C and C++ compiler that ships with musl. It is hermetic, so no system libc is needed, and cross-building for ARM works without a separate toolchain. There are examples of dropping it into CMake and Makefile through the CC and CXX variables.

A table maps each optimization mode to its -O0, -O2, -O3 or -Os equivalent, and another one decodes common errors, from type mismatches to unused variables, which Zig makes you discard explicitly with _ = x;. ReleaseFast strips safety checks and turns them into undefined behaviour, so the author advises validating a build under ReleaseSafe first. For projects with build.zig, the skill points to companion sections on the build system and C interop.

What it helps with

  • build an executable from a single main.zig file
  • choose between ReleaseSafe and ReleaseFast for production
  • compile C code with zig cc and no system toolchain
  • read a Zig error and find the type mismatch
  • check syntax before a full compile

Example prompts for the agent

  • How do I build src/main.zig into an executable named myapp?
  • Which optimization mode should I use for production, and how does ReleaseSafe differ from ReleaseFast?
  • Compile a C file for aarch64-linux-gnu with zig cc

How to install zig-compiler

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill zig-compiler -a claude-code

2Manually: put the files in place

~/.claude/skills/zig-compiler/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/zig-compiler/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about zig-compiler

How do I install zig-compiler in Claude?

Run npx skills add mohitmishra786/low-level-dev-skills --skill zig-compiler -a claude-code. The skill lands in ~/.claude/skills/zig-compiler/SKILL.md.

What does zig-compiler do?

It tells the agent how to invoke the Zig compiler: output types, optimization modes, cross-compilation, zig cc for C and C++, and how to read error messages.

How is zig cc different from gcc and clang?

It needs no system toolchain, cross-compiles to any supported target, and bundles musl for static Linux builds.

Source text

SKILL.md · MIT
---
name: zig-compiler
description: Zig compiler skill for systems programming. Use when compiling Zig programs, selecting optimization modes, using zig cc as a C compiler, reading Zig error messages, or understanding Zig's compilation model. Activates on queries about zig build-exe, zig build-lib, optimize modes, ReleaseSafe, ReleaseFast, ReleaseSmall, zig cc, zig ast-check, or Zig compilation errors.
---

# Zig Compiler

## Purpose

Guide agents through Zig compiler invocation: optimization modes, output types, `zig cc` as a C compiler drop-in, error message interpretation, and the Zig compilation pipeline.

## Triggers

- "How do I compile a Zig program?"
- "What are Zig's optimization modes and when do I use each?"
- "How do I use zig cc to compile C code?"
- "How do I read Zig error messages?"
- "How do I compile a Zig library?"
- "What is zig ast-check?"

## Workflow

### 1. Basic compilation

```bash
# Compile and run a single file
zig run src/main.zig

# Compile to executable
zig build-exe src/main.zig

# Compile to static library
zig build-lib src/mylib.zig

# Compile to shared library
zig build-lib src/mylib.zig -dynamic

# Compile to object file
zig build-obj src/main.zig

# Output name
zig build-exe src/main.zig -femit-bin=myapp
```

### 2. Optimization modes

| Mode | Flag | `-O` equiv | Purpose |
|------|------|-----------|---------|
| `Debug` (default) | `-O Debug` | `-O0 -g` | Fast compile, all safety checks, debug info |
| `ReleaseSafe` | `-O ReleaseSafe` | `-O2 + checks` | Optimized with safety checks retained |
| `ReleaseFast` | `-O ReleaseFast` | `-O3` | Maximum speed, safety checks removed |
| `ReleaseSmall` | `-O ReleaseSmall` | `-Os` | Minimize binary size |

```bash
zig build-exe src/main.zig -O Debug        # dev builds
zig build-exe src/main.zig -O ReleaseSafe  # production with safety
zig build-exe src/main.zig -O ReleaseFast  # max performance
zig build-exe src/main.zig -O ReleaseSmall # embedded/WASM
```

Safety checks in `Debug` and `ReleaseSafe`:
- Integer overflow → detected and panics with source location
- Array bounds checking → panics on OOB
- Null pointer dereference → panic (not crash)
- `unreachable` → panic
- Enum tag validation → panic on bad cast

`ReleaseFast`: turns safety checks into `undefined behaviour` (same semantics as C `-O3`). Use only when you've validated with `ReleaseSafe` first.

### 3. Target specification

```bash
# List all supported targets
zig targets

# Cross-compile for specific target
zig build-exe src/main.zig \
    -target aarch64-linux-gnu \
    -O ReleaseFast

# Embedded (no OS)
zig build-exe src/main.zig \
    -target thumb-freestanding-eabi \
    -O ReleaseSmall

# WebAssembly
zig build-exe src/main.zig \
    -target wasm32-freestanding \
    -O ReleaseSmall \
    --export=main

# Common target triples: cpu-os-abi
# x86_64-linux-gnu, x86_64-windows-gnu, aarch64-macos-none
# thumbv7m-freestanding-eabi, wasm32-wasi, wasm32-freestanding
```

### 4. zig cc — C compiler drop-in

Zig ships a C/C++ compiler (`zig cc` / `zig c++`) backed by Clang and musl. It is hermetic — no system libc required.

```bash
# Compile C code
zig cc -O2 -Wall main.c -o myapp

# Compile C++ code
zig c++ -std=c++17 -O2 main.cpp -o myapp

# Cross-compile C for ARM (no cross toolchain needed!)
zig cc -target aarch64-linux-gnu -O2 main.c -o myapp-arm

# Statically link with musl
zig cc -target x86_64-linux-musl main.c -o myapp-static

# Use in CMake (override compiler)
CC="zig cc" CXX="zig c++" cmake -S . -B build
cmake --build build

# Use in Makefile
CC="zig cc" make
```

`zig cc` advantages over gcc/clang:
- No system toolchain required (fully hermetic)
- Built-in cross-compilation for any supported target
- Always ships with a recent clang version
- musl libc bundled for static Linux builds

### 5. Emit formats

```bash
# Emit LLVM IR
zig build-exe src/main.zig --emit-llvm-ir

# Emit assembly
zig build-exe src/main.zig --emit-asm
cat main.s

# Emit binary and assembly
zig build-exe src/main.zig -femit-bin=myapp -femit-asm=myapp.s
```

### 6. AST check and syntax validation

```bash
# Check syntax without compiling
zig ast-check src/main.zig

# Format code
zig fmt src/main.zig
zig fmt src/          # format entire directory

# Check formatting without modifying
zig fmt --check src/

# Tokenize (debugging zig fmt)
zig tokenize src/main.zig
```

### 7. Reading Zig error messages

Zig error messages include:
- Source file and line
- Column indicator with arrow
- Note messages for context

```
src/main.zig:10:5: error: expected type 'u32', found 'i32'
    x: i32 = 5,
    ^
src/main.zig:7:5: note: struct field 'x' declared here
    x: u32,
    ^
```

Key error patterns:
| Error | Meaning |
|-------|---------|
| `error: expected type 'X', found 'Y'` | Type mismatch |
| `error: use of undeclared identifier 'X'` | Missing import or typo |
| `error: integer overflow` | Comptime overflow (caught at compile) |
| `error: cannot assign to constant` | Mutating a `const` variable |
| `error: unused variable 'x'` | All variables must be used |
| `error: unused function parameter 'x'` | Use `_ = x;` to suppress |

Suppress "unused" errors:
```zig
_ = unused_variable;  // explicitly discard
```

### 8. Version and environment

```bash
# Check Zig version
zig version

# Show build configuration
zig env

# Show standard library location
zig env | grep lib_dir
```

For optimization mode details and target triple reference, see [references/zig-optimize-modes.md](references/zig-optimize-modes.md).

## Related skills

- Use `skills/zig/zig-build-system` for multi-file projects with build.zig
- Use `skills/zig/zig-cinterop` for calling C from Zig and vice versa
- Use `skills/zig/zig-cross` for cross-compilation targets and sysroots
- Use `skills/zig/zig-debugging` for GDB/LLDB with Zig binaries

© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills

Similar skills

All skills for Programming languages →

From the same set62

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor