Working with C
Zig reads C headers directly. Thus, for interop, you must know which types cross the boundary and how they cross it. These chapters show translate-c and what replaced @cImport, and the C pointer types and what they refuse to do. They also show the primitive type mapping, and how to export a C ABI that 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 that translate-c has a type to emit. At the boundary, change[*c]Tto a real pointer or a slice. Do not let[*c]Tspread.- A plain
structhas no guaranteed layout, and Zig can reorder its fields. A struct that crosses to C needsextern structorpacked struct.
4 chapters.
- ABIextern and packed, for layouts with a guarantee.
- C Primitive TypesTypes whose width follows the target's C ABI.
- C Pointers[*c]T: the pointer type translate-c emits, and why to convert it at the boundary.
- Import Ctranslate-c, and calling C without bindings.