In reading the documentation for zig, I was under the impression that zig could compile both C and C++ code. Consequently, I thought you could import a C++ file's header via @cImport
and have zig build
succeed. However, I can't seem to get this to work for a C++ library integration.
I first create my project, zig init-lib
and then add my import to src/main.zig
via the @cImport
directive. Specifically, I @cInclude("hooks/hooks.h")
the C++ header file of this library. If I attempt to zig build
at this point, the build fails, unable to find the header. I fix this by modifying build.zig
to lib.addIncludeDir("/usr/include/library")
.
Since this C++ library is now being parsed and uses the C++ standard library, the next error I get when I zig build
is that the stdexcept
header is not found. To fix this, I modify build.zig
to lib.linkSystemLibrary("c++")
.
Lastly, and the error I'm stuck on now, is an assortment of errors in /path/to/zig-linux-x86_64-0.9.1/lib/libcxx/include/<files>
. I get stuff like unknown type name '__LIBCPP_PUSH_MACROS
, unknown type name 'namespace'
, or unknown type name 'template'
.
Googling this, the only thing of partial relevance that I could find was that this is due to clang's default interpretation of .h files is as C files which obviously don't have namespace
or template
keywords, but I don't know what to do with that knowledge. LLVM on MacOs - unknown type name 'template' in standard file iosfwd
Does anyone have any insight as to how to actually integrate with a C++ (not pure C) library through zig?