|
1 | 1 | # Translate-C
|
2 | 2 |
|
3 |
| -A Zig package for translating C code into Zig code intended to replace `@cImport` and `zig translate-c`. |
| 3 | +A Zig package for translating C code into Zig code, intended to replace `@cImport` and `zig translate-c`. |
4 | 4 |
|
5 |
| -## Usage examples |
| 5 | +## Usage |
| 6 | + |
| 7 | +Add `translate-c` to your `build.zig.zon` with this command: |
| 8 | + |
| 9 | +```sh-session |
| 10 | +$ zig fetch --save git+https://github.com/ziglang/translate-c |
| 11 | +info: resolved to commit 1aa9ec052415feeaa0494190ae35a94849a24399 |
| 12 | +``` |
| 13 | + |
| 14 | +Then, within your `build.zig`, write something like this: |
| 15 | + |
| 16 | +```zig |
| 17 | +// An abstraction to make using translate-c as simple as possible. |
| 18 | +const Translator = @import("translate_c").Translator; |
| 19 | +
|
| 20 | +// You *can* pass `target` and/or `optimize` in the options struct here, but it's typically |
| 21 | +// not necessary. You usually want to build for the host target, which is the default. |
| 22 | +const translate_c = b.dependency("translate_c", .{}); |
| 23 | +
|
| 24 | +const t: Translator = .init(translate_c, .{ |
| 25 | + .c_source_file = b.path("to_translate.h"), |
| 26 | + .target = target, |
| 27 | + .optimize = optimize, |
| 28 | +}); |
| 29 | +// If you want, you can now call methods on `Translator` to add include paths (etc). |
| 30 | +
|
| 31 | +// Depend on the translated C code as a Zig module. |
| 32 | +some_module.addImport("translated", t.mod); |
| 33 | +// ...or, if you want to, just use the output file directly. |
| 34 | +const translated_to_zig: LazyPath = t.output_file; |
| 35 | +``` |
| 36 | + |
| 37 | +For a more complete usage, take a look at the [Examples](#examples). |
| 38 | + |
| 39 | +## Examples |
| 40 | + |
| 41 | +This repository contains a few examples in the `examples/` directory. You can test that all of the examples |
| 42 | +work by running `zig build all` in that directory. |
| 43 | + |
| 44 | +Within a specific example's directory, run `zig build test` to test that example. Most also have a step |
| 45 | +called `run` or similar which you can use to run the compiled program without hiding stdout. |
6 | 46 |
|
7 | 47 | - [import header](examples/import_header/build.zig)
|
8 | 48 | - [compile c](examples/compile_c/build.zig)
|
| 49 | +- [use static library](examples/use_static_lib/build.zig) |
0 commit comments