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.
- All c++ code is complied by g++
- C++ name mangling can be solved in following way
const cppLibAddFun = @extern(*const fn (i64, i64) callconv(.C) i64, .{ .name = "_Z12cppLibAddFunii" });more info ziglang/zig#19999 - 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 thelibstdc++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
- Install
zigandbuild-essentialto have access tog++and gnumake - In Makefile setup
ZIGcompiler version or path to zig bin - Run
make allto complile all targets