A Zig package for translating C code into Zig code.
- Zig 73.1%
- C 26.9%
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]> |
||
|---|---|---|
| .forgejo/workflows | ||
| build | ||
| examples | ||
| lib | ||
| src | ||
| test | ||
| .gitattributes | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| LICENSE | ||
| README.md | ||
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.