Skip to content

Commit 9abc79f

Browse files
committed
README.md: elaborate
* Add basic usage instructions * Explain how to run examples * Add link to new example (added in #24)
1 parent 1aa9ec0 commit 9abc79f

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
11
# Translate-C
22

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`.
44

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.
646

747
- [import header](examples/import_header/build.zig)
848
- [compile c](examples/compile_c/build.zig)
49+
- [use static library](examples/use_static_lib/build.zig)

0 commit comments

Comments
 (0)