Open-Source Wikis

/

Zig

/

Applications

/

translate-c

ziglang/zig

translate-c

Active contributors: vexu, andrewrk

Purpose

zig translate-c converts a C source file or .c-style translation unit into Zig. The work is split between Aro (the bundled C frontend, see aro) and lib/compiler/translate-c/, which walks Aro's AST and emits Zig.

Directory layout

lib/compiler/translate-c/
└── ...                 # AST-to-Zig lowering for every Aro node kind

The driver entry point is src/main.zig's cmdTranslateC (and friends), which builds an Aro translation unit, runs the lowering, and prints the resulting Zig source.

What gets translated

zig translate-c aims to produce compilable Zig from any preprocessed C input:

  • Type declarations (struct, union, enum, typedef).
  • Function declarations and definitions.
  • Macros (constant #defines become Zig consts; function-like macros are lowered when feasible).
  • Statements and expressions (with the usual caveats for unrepresentable patterns).
  • C-style bitfields, anonymous fields, flexible arrays.

When a construct cannot be translated (e.g. some macros, certain preprocessor abuses, GCC extensions), the generator emits @compileError or skips the symbol with a comment, matching the behavior of @cImport.

How it integrates

  • Standalone: zig translate-c foo.c > foo.zig.
  • From within Zig: @cImport({ @cInclude("foo.h"); }) runs the same path internally and returns a Zig namespace.
  • From the build system: lib/std/Build/Step/TranslateC.zig exposes a Step.TranslateC.

Tests

zig build test-translate-c exercises a fixture set under test/cases.zig's translate-c slice. Each fixture pairs a C input with the expected Zig output.

Key source files

File Purpose
lib/compiler/translate-c/ The translator.
lib/compiler/aro/ The C frontend it consumes.
lib/std/Build/Step/TranslateC.zig The build step.
src/main.zig (cmdTranslateC) CLI dispatch.

See aro for the upstream C frontend.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

translate-c – Zig wiki | Factory