Demonstrates generating Dart FFI bindings from Zig source code using the
bindings CLI command. The generated file provides typed structs and function
bindings — no hand-written @Native annotations needed.
- Dart SDK ^3.13.0
- Zig 0.15.2 or 0.16.0 installed and available on
PATH
From the example/bindings directory:
dart run native_toolchain_zig:zig bindings \
--zig-dir zig \
--root-source-file src/counter.zig \
--output lib/ffi.g.dartOr from the repository root:
dart run native_toolchain_zig:zig bindings \
--package-root example/bindings \
--zig-dir zig \
--root-source-file src/counter.zig \
--output lib/ffi.g.dartUse --watch to regenerate automatically when Zig source files change:
dart run native_toolchain_zig:zig bindings \
--zig-dir zig \
--root-source-file src/counter.zig \
--output lib/ffi.g.dart \
--watchdart run bindings:mainzig/src/counter.zigdefines exported functions (export fn) and anextern struct(Counter).- The
bindingsCLI extracts the exported ABI surface from Zig source, then generateslib/ffi.g.dart. C imports usezig translate-c. lib/bindings.dartwraps the raw FFI calls in a Dart-friendlyCounterWrapperclass.- The build hook (
hook/build.dart) compiles the Zig code into a native library and registers it as a code asset. bin/main.dartcalls both the raw bindings and the wrapper to demonstrate usage.