1
0
Fork
You've already forked translate-c
0
forked from ziglang/translate-c
A Zig package for translating C code into Zig code.
  • Zig 71.1%
  • C 28.9%
2026年07月12日 01:03:31 +03:00
.forgejo/workflows CI: disable run examples step 2026年07月12日 01:03:31 +03:00
build clarify optimization modes in README.md 2026年06月19日 02:56:08 +03:00
examples Fix link paths in examples/README.md 2026年06月16日 13:30:28 +03:00
lib mark int casting helper functions inline 2026年07月04日 17:53:12 +03:00
src LibCDirs now takes a zig_lib_dir as a Build.Cache.Path instead of a []const u8 2026年07月05日 21:34:11 +03:00
test mark int casting helper functions inline 2026年07月04日 17:53:12 +03:00
.gitattributes
.gitignore Update Aro and Zig 2026年02月12日 16:23:33 +02:00
build.zig build: don't link libc 2026年07月08日 20:25:08 -07:00
build.zig.zon update minimum_zig_version in build.zig.zon 2026年07月10日 11:41:12 +03:00
LICENSE
README.md do not use allocators from std.init 2026年06月19日 10:58:24 +03:00

Translate-C

A Zig package for translating C code into Zig code, intended to replace @cImport and zig translate-c.

This is the main branch, which tracks master branch of Zig. Other branches track other versions of Zig.

Usage

Add translate-c to your build.zig.zon with this command:

$ zig fetch --save git+https://codeberg.org/ziglang/translate-c
info: resolved to commit 1aa9ec052415feeaa0494190ae35a94849a24399

Then, within your build.zig, write something like this:

// An abstraction to make using translate-c as simple as possible.constTranslator=@import("translate_c").Translator;consttranslate_c=b.dependency("translate_c",.{});constt:Translator=.init(translate_c,.{.c_source_file=b.path("to_translate.h"),.target=target,// This is the optimization mode of the C code being translated and// the resulting Zig code..optimize=optimize,// more options go here (see below)});// If you want, you can now call methods on `Translator` to add include paths (etc).// Depend on the translated C code as a Zig module.some_module.addImport("translated",t.mod);// ...or, if you want to, just use the output file directly.consttranslated_to_zig:LazyPath=t.output_file;

For a more complete usage, take a look at the examples/ directory.

Options

/// Should static functions be translated as `pub`.pub_static:bool,/// Should function bodies be translated.func_bodies:bool,/// Should macro names of literals be preserved.keep_macro_literals:bool,/// Should struct fields be default initialized.default_init:bool,/// Control when to treat a trailing array as a flexible array member./// Mirrors the -fstrict-flex-arrays=<n> compiler flag.strict_flex_arrays:StrictFlexArraysLevel,pubconstStrictFlexArraysLevel=enum{/// Any trailing array member is a flexible array.@"0",/// Trailing arrays of size 0, 1, or undefined are flexible.@"1",/// Trailing arrays of size 0 or undefined are flexible (default).@"2",/// Only trailing arrays of undefined size are flexible.@"3",};