A Zig package for translating C code into Zig code.
  • Zig 73.1%
  • C 26.9%
Find a file
robbielyman d2be2f19ef
All checks were successful
ci / build (x86_64-freebsd) (push) Successful in 3m53s
ci / build (x86_64-windows) (push) Successful in 5m52s
ci / build (aarch64-macos) (push) Successful in 7m24s
ci / build (x86_64-linux) (push) Successful in 9m58s
fix: allow creating string literals via macro expansion
This PR resolves an error where the following macro could not be parsed
by translate-c, but appeared in Lua 5.1 and LuaJIT source code.

```c
#define FAILS(x) "'" x "'"
```

closes #282

Co-authored-by: Robbie Lyman <[email protected]>
Reviewed-on: #291
Co-authored-by: robbielyman <[email protected]>
Co-committed-by: robbielyman <[email protected]>
2026-02-13 11:37:36 +01:00
.forgejo/workflows ci: update setup-zig to v2.2.1 2026-01-19 12:37:32 +01:00
build Update Aro and Zig 2026-01-25 12:52:19 +01:00
examples build: Pass -fno- flags correctly 2025-11-30 16:44:54 -08:00
lib update Aro and Zig 2025-11-26 18:31:21 +02:00
src fix: allow creating string literals via macro expansion 2026-02-13 11:37:36 +01:00
test fix: allow creating string literals via macro expansion 2026-02-13 11:37:36 +01: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 Aro and Zig 2026-02-12 16:23:33 +02:00
LICENSE init 2024-11-06 00:52:11 +02:00
README.md github -> codeberg 2025-11-28 03:47:22 +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://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.
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.