Skip to content

kj-crypto/zig-cpp-interop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Zig interoperability with g++ compiled code

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published