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.6%
  • C 28.4%
2026年05月02日 19:18:24 -07:00
.forgejo/workflows Revert "ci: work around Codeberg/Community#2320 " 2026年03月16日 12:31:48 +01:00
build build: respect --search-prefix in Translator 2026年04月20日 15:03:20 +02:00
examples update to Zig 0.16.0 2026年04月24日 04:13:24 +02:00
lib Update Aro and Zig ( #305 ) 2026年03月17日 16:20:00 +01:00
src update to zig 0.17.0-dev.251+0db721ec2 2026年05月02日 19:13:17 -07:00
test test: expand the set of cross targets 2026年04月24日 05:22:55 +02:00
.gitattributes prevent git from mangling line endings in C files 2024年11月18日 12:29:48 +02:00
.gitignore Update Aro and Zig 2026年02月12日 16:23:33 +02:00
build.zig update Aro and Zig 2025年12月24日 08:55:05 +08:00
build.zig.zon update to zig 0.17.0-dev.251+0db721ec2 2026年05月02日 19:13:17 -07:00
LICENSE init 2024年11月06日 00:52:11 +02:00
README.md README: add comment about options 2026年05月02日 19:18:24 -07: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;// You *can* pass `target` and/or `optimize` in the options struct here, but it's typically// not necessary. You usually want to build for the host target, which is the default.consttranslate_c=b.dependency("translate_c",.{});constt:Translator=.init(translate_c,.{.c_source_file=b.path("to_translate.h"),.target=target,.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.

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",};

Examples

This repository contains a few examples in the examples/ directory. You can test that all of the examples work by running zig build all in that directory.

Within a specific example's directory, run zig build test to test that example. Most also have a step called run or similar which you can use to run the compiled program without hiding stdout.