Skip to content

Latest commit

 

History

History
23 lines (20 loc) · 1.08 KB

File metadata and controls

23 lines (20 loc) · 1.08 KB

The Zig C/C++ interoperability minimal working example

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.

Key points

  1. All c++ code is complied by g++
  2. 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
  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

Build and run

  1. Install zig and build-essential to have access to g++ and gnu make
  2. In Makefile setup ZIG compiler version or path to zig bin
  3. Run make all to complile all targets