Hey there, I’ve been playing around with Zig and want to try using a cpp library. Unfortunately I’m having a bit of a struggle getting it working even after follow a few other threads I found in the forum.
I’ve just set up my project with zig init and then zig fetch --save=jolt git+https://github.com/jrouwe/JoltPhysics.git#v5.6.0 and I’ve added the following to my build.zig.
// I've added the fetched dependency here and tried setting up translate c
const joltd = b.dependency("jolt", .{});
const joltc = b.addTranslateC(.{
.optimize = optimize,
.target = target,
.root_source_file = joltd.path("Jolt/Jolt.h"),
.link_libc = true,
});
joltc.addIncludePath(joltd.path("."));
const jolt = joltc.createModule();
// I thought adding `link_libcpp` here would get it working
jolt.link_libcpp = true;
const exe = b.addExecutable(.{
.name = "zig_project",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
// Enabling links doesn't seems to help here either
.link_libc = true,
.link_libcpp = true,
.imports = &.{
.{ .name = "zig_project", .module = mod },
// I've added the translated module here
.{ .name = "jolt", .module = jolt },
},
}),
});
I’m not familiar with c/cpp nor the zig build-system here, so I’m struggling to figure it out. From what I understand <new> is a cpp header, but it doesn’t seem like I get the option to link to libcpp when using translate-c (or at least the link_libcpp options I provided have no effect).
I haven’t tried the new translate-c library either, but from what I can tell by a brief look is that the options are essentially the same.
JoltC sounds like what I need here, but I was hoping to be able to use other cpp libraries if I ever need to.
I saw some other projects/threads I was reading (example) that uses cpp. Is it just a case that translate-c will only support a c wrapper? Or only a subset of cpp features? Is there a good automated way to generate a c wrapper?
I’m unsure as I’m not checking the code paths locally but I think that code is still translating the code as C - webview.cc is simply an include that follows through eventually to api.h.
Looks like webview is written to compile in C and C++, and unless I’m mistaken Zig will be going down the C route, with those -std=c++11 flags not used during the translate step.
edit: @floooh is more correct below, but the point is basically the same
The webview example works because the webview.h header exposes a C API. This C header is processed via translate-c to generate a Zig API shim:
The webview implementation itself is a C++ file and is compiled via Zig’s Clang frontend into a static library here (the addCSourceFile is misleading here, it should be called addCppSourceFile):
…this is quite typical for integrating C++ libraries with Zig, but the precondition is that the C++ library offers a C API in a plain-C header (Zig can’t directly talk to C++ APIs, only C APIs), and just using translate-c isn’t enough since it cannot translate C++ code, you also need Zig’s integrated Clang frontend in order to build C++ code into a static library.