inspeired by this article uses zig to create zig bindgens for c++
zig cc -x c++ -std=c++11 -Xclang -ast-dump=json {input_file}
- Glue is invitable ... snap ... and needed to make the bindings cross platform
- Transpiled code can leak memory (implicit destructors aren't called)
- Manual adjustments are required, specially to make bindings idiomatic and easer to read
- Meant to work with libraries that follow the C with classes coding style
- No Zig side inheritance, instead create bindings of a C++ implementation
- make sure
zigin your PATH, you will need the version0.13and up - build the project and copy the
c2zexecutable fromzig-outto your desired location - or just run the project using
zig build run -- ARGS
zig build run -- lib.horc2z lib.h- pass any clang argument like include and defines e.g.
zig build run -- -DNDEBUG -I.\include -target x86-linux -- .\include\lib.h - modify the generated bindings until it works ;) you might need to import
cpp.zigit is located in the src folder
msvchas a second tier support, just pass it as target tuple like as:-no-glue -target x86_64-windows-msvcto generate a target specifc binding for it. Debug builds aren't fully supported, useReleaseFastor at least-O1, you might also wan't to find a way of define_ITERATOR_DEBUG_LEVELto something different than2.
-
transpile inline or constexpr constructors when the class isn't polymorphic
-
use function pointers to handle varidact functions
-
(hard)
#include->@import -
(easy) walk a directory tree
-
transpile vector of vectors
-
(easy) better input file not found error
-
(easy) verbose option
-
(easy) handle
BlockCommandCommentandParamCommandCommentinFullComment -
(easy) resolve return of function with a aliased return type
-
(easy) fail when clang ast-bump has failed, because missing headers or wrong code
-
(hard)
if (*data++ == v) { ... }should generate{ const __tmp0 = data; data += 1; if (__tmp0.* == v) { ... } } -
(hard) solve
UnresolvedMemberExpr, maybe when integrating clang ast directly -
(hard) solve implicit destructors calls
-
(easy) handle private members, class is default private, struct default public, in code is referenced as
self.public -
(hard) handle varidact functions
myFunction(va_args) -> myFunction(args: [*c]u8) and myFunction__VA(...) -
(hard) apply
keywordFix -
use
@compileErrorfor objects that couldn't be transpiled -
write layout tests
- fpng (99.99 %)
- xatlas (86.40 %)
- imgui (67.21 %)
- msdfgen (~80% ish)
- box2d
- basis_universal
- JoltPhysics
- astc-encoder
- SDL2
- stbi
- raylib
- meshoptimizer
- tinyexr
- minimp3