A Zig package for translating C code into Zig code.
c zig
  • Zig 74.2%
  • C 25.8%
Find a file
RedStealthDev 0393a7d005
Some checks failed
CI / build (ubuntu-latest) (push) Failing after 4m58s
CI / build (macos-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
fixed link to arroc
2025-09-22 18:58:32 +02:00
.github/workflows ci: don't test every build mode 2025-06-19 21:22:41 +01:00
build Translator: add depfile generation 2025-07-28 13:03:50 +01:00
examples remove uses of managed containers 2025-08-29 15:44:23 +00:00
lib remove uses of managed containers 2025-08-29 15:44:23 +00:00
src update to Zig 0.16.0-dev.233+a0ec4e270 2025-09-14 16:22:16 +03:00
test update to 0.15.1 2025-09-22 18:30:45 +02:00
.gitattributes prevent git from mangling line endings in C files 2024-11-18 12:29:48 +02:00
.gitignore init 2024-11-06 00:52:11 +02:00
build.zig update to writergate changes 2025-07-19 13:34:37 +03:00
build.zig.zon fixed link to arroc 2025-09-22 18:58:32 +02:00
LICENSE init 2024-11-06 00:52:11 +02:00
README.md README.md: elaborate 2025-06-20 00:07:27 +01:00

Translate-C

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

Usage

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

$ zig fetch --save git+https://github.com/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.
const Translator = @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.
const translate_c = b.dependency("translate_c", .{});

const t: Translator = .init(translate_c, .{
    .c_source_file = b.path("to_translate.h"),
    .target = target,
    .optimize = optimize,
});
// 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.
const translated_to_zig: LazyPath = t.output_file;

For a more complete usage, take a look at the Examples.

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.