This demo shows how to use build.zig
to compile a C++
project and call C++
lib function in Zig
. It includes everything you need to know about when dealing
with a C++
project with Zig
.
It's worth it to point out that @cImport
and @cInclude
only works for C
headers, it doesn't work for C++
header, that's why you have to write a wrapper
header file if you want to include a C++
lib header file!
src/utils/a.h
and src/utils/b.h
mix the C++
and wapper in the same header
file, that's why it works.
The project has the following folder structure:
.
├── README.md
├── build #`cmake` build folder
├── build.zig # `Zig` build configuration file
├── CMakeLists.txt # `cmake` configuration file
├── configure.sh # Configure `cmake`
├── run.sh # Build `main.cpp` with `cmake` and run
├── build_cpp.sh # Build `main.cpp` with `clang++`
├── build_c.sh # Build `main.c` with `clang`
└── src
├── main.cpp # `C++` calls namespace functions in `liba.so` and `lib.so`
├── main.c # `C` calls namespace functions in `liba.so` and `lib.so`
├── main.zig # `Zig` calls namespace functions in `liba.so` and `lib.so`
└── utils # Lib `a` and `b`
├── a.cpp
├── a.h
├── b.cpp
└── b.h
cmake
builds the build/liba.so
and build/lib.so
first, then builds the
./build/cpp
and links it to the build/liba.so
and build/libb.so
.
./configure.sh
./run.sh
# >>> Simple zig build CPP demo.
# >>> [ A - Result ] {
# {
# invisible_value: 28
# is_success: true
# }
# >>> A::Result destroyed.
# >>> [ B - Result ]
# {
# invisible_float: 88.456
# }
# >>> B::Result destroye
This only works once you build the project via cmake
, as it doesn't build
liba.so
and lib.so
but just links to them!!!
./build_cpp.sh && LD_LIBRARY_PATH=./build ./build/cpp_2
# >>> Simple zig build CPP demo.
# >>> [ A - Result ] {
# {
# invisible_value: 28
# is_success: true
# }
# >>> A::Result destroyed.
# >>> [ B - Result ]
# {
# invisible_float: 88.456
# }
# >>> B::Result destroye
This only works once you build the project via cmake
, as it doesn't build
liba.so
and lib.so
but just links to them!!!
./build_c.sh && LD_LIBRARY_PATH=./build build/c
# >>> Calling C++ lib function in C:)
#
# >>> a_result value: 38
# >>> a_result is_success: true
# >>> [ A - Result ] {
# {
# invisible_value: 38
# is_success: true
# }
# >>> A::Result destroyed.
#
# >>> b_result value: 123.789
# >>> [ B - Result ]
# {
# invisible_float: 123.789
# }
# >>> B::Result destroyed.
This only works once you build the project via cmake
, as it doesn't build
liba.so
and lib.so
but just links to them!!!
-
Build and run C++ binary (
zig-out/bin/cpp_binary
)zig build run-cpp # >>> Simple zig build CPP demo. # >>> [ A - Result ] { # { # invisible_value: 28 # is_success: true # } # >>> A::Result destroyed. # >>> [ B - Result ] # { # invisible_float: 88.456 # } # >>> B::Result destroyed.
-
Build and run zig binary (
zig-out/bin/zig_binary
)zig build run-zig # >>> Calling C++ lib function in Zig:) # # >>> a_result value: 999 # >>> a_result is_success: true # >>> [ A - Result ] { # { # invisible_value: 999 # is_success: true # } # >>> A::Result destroyed. # # >>> b_result value: 55.456>>> [ B - Result ] # { # invisible_float: 55.456 # } # >>> B::Result destroyed.