kj-crypto/zig-cpp-interop
Zig interoperability with g++ compiled code
The purpose of this example was to establish interoperability between c++ code and zig. Key issue is to use non llvm c++ compiler to create static lib and the use it and resolve linking symbols using zig.
const cppLibAddFun = @extern(*const fn (i64, i64) callconv(.C) i64, .{ .name = "_Z12cppLibAddFunii" });
more info https://github.com/ziglang/zig/issues/19999
3. Zig uses clang under the hood and it defaul links against libc++ which differs
from gnu libstdc++
.
To solve this issue we need explicity pass the libstdc++
lib and use -lc++
linker flag.
Example:
zig build-exe example_2.zig libzig_static.a /lib/x86_64-linux-gnu/libstdc++.so.6 --name example-2 -lc++ -I${PWD}/lib
zig
and build-essential
to have access to g++
and gnu make
ZIG
compiler version or path to zig binmake all
to complile all targets