Working with C
Zig reads C headers directly, so interop is a matter of knowing which types cross the boundary and how. These chapters cover @cImport and translate-c, the C pointer types and what they refuse to do, the primitive type mapping, and exporting a C ABI other languages can link against.
- Zig reads the C header itself. There is no binding file to generate, commit, and then forget to regenerate.
[*c]Texists so translate-c has something to emit. Turn it into a real pointer or a slice at the boundary and do not let it spread.- A plain
structhas no guaranteed layout and Zig may reorder its fields. Anything crossing to C needsextern structorpacked struct.
4 chapters.
- ABIextern and packed, for layouts you can rely on.
- C Primitive TypesTypes whose width follows the target's C ABI.
- C Pointers[*c]T: the escape hatch, and why to leave it at the door.
- Importing C@cImport, and calling C without bindings.