https://github.com/ssteinbach/c_call_zig
Small example of calling a static zig library from C code.
https://github.com/ssteinbach/c_call_zig
c ffi linking zig ziglang
Last synced: 11 months ago
JSON representation
Small example of calling a static zig library from C code.
- Host: GitHub
- URL: https://github.com/ssteinbach/c_call_zig
- Owner: ssteinbach
- Created: 2022-10-13T06:00:14.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T05:18:33.000Z (about 1 year ago)
- Last Synced: 2025-04-08T06:24:58.779Z (about 1 year ago)
- Topics: c, ffi, linking, zig, ziglang
- Language: Zig
- Homepage:
- Size: 21.5 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Calling Zig from C
## Overview
Example repository for calling zig functions from C. Zig has some nice
reflection based JSON parsing ability, this exposes a c function that uses that
to parse a C-struct out of JSON and return it.
## Requirements
* Zig (built against 0.13.0)
* C toolchain (clang)
## Building
```bash
> make
> ./c_to_zig_json_reader
```
## Testing
There is a (small) unit test in `src/json_reader.zig`.
```bash
> make test
```
## Build.zig Note
When building the static library to wrap in C, make sure the build.zig
includes:
```zig
const lib = b.addStaticLibrary("zigJsonReader", "json_reader.zig");
lib.bundle_compiler_rt = true;
```
Additionally, the header file is written by hand.