Zig C Interop
Purpose
Guide agents through Zig's C interoperability: @cImport/@cInclude for calling C, translate-c for header inspection, extern struct and packed struct for ABI-compatible types, exporting Zig for C consumption, and zig cc for mixed C/Zig builds.
Triggers
- "How do I call a C function from Zig?"
- "How do I use @cImport and @cInclude?"
- "How do I export Zig functions to be called from C?"
- "How do I define a struct that matches a C struct?"
- "What does translate-c do?"
- "How do I build a mixed C and Zig project?"
Workflow
1. Calling C from Zig with @cImport
In build.zig:
2. translate-c — inspect C header translation
translate-c converts C headers to Zig declarations, letting you see exactly how Zig sees a C API:
This is Zig's equivalent of bindgen — you use it to understand what Zig generates, then use @cImport directly in code.
3. C type mapping
4. extern struct — ABI-compatible structs
Use extern struct to match a C struct's memory layout exactly:
5. packed struct — bit-level layout
6. Exporting Zig to C
Generate a C header (manual or with tools):
7. Calling Zig from C in build.zig
8. Opaque types and forward declarations
9. Variadic functions
For translate-c output guide and C ABI types reference, see references/translate-c-guide.md [blocked].
Related skills
- Use
skills/zig/zig-compilerforzig ccC compilation and basic Zig builds - Use
skills/zig/zig-build-systemforbuild.zigwith mixed C/Zig projects - Use
skills/binaries/elf-inspectionto verify symbol exports and ABI - Use
skills/rust/rust-ffifor comparison with Rust's C FFI approach


