How to include libcpp with addTranslateC

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 },
            },
        }),
    });
error: translation failure
/home/noah/Projects/zig-project/zig-pkg/N-V-__8AAFcdJwIbbDnlYvqauoynAkXD36B8fLtw1-yybF9B/./Jolt/Core/Core.h:501:10: error: 'new' not found
#include <new>
         ^
error: 2 compilation errors
failed command: /home/noah/.local/share/mise/installs/zig/0.16.0/zig translate-c -lc --cache-dir .zig-cache --global-cache-dir /home/noah/.cache/zig -I /home/noah/Projects/zig-project/zig-pkg/N-V-__8AAFcdJwIbbDnlYvqauoynAkXD36B8fLtw1-yybF9B/. /home/noah/Projects/zig-project/zig-pkg/N-V-__8AAFcdJwIbbDnlYvqauoynAkXD36B8fLtw1-yybF9B/Jolt/Jolt.h --listen=-

Build Summary: 0/4 steps succeeded (1 failed)
install transitive failure
└─ install zig_project transitive failure
   └─ compile exe zig_project Debug native transitive failure
      └─ translate-c 2 errors

error: the following build command failed with exit code 1:

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.

translate-c only translates C code, not C++. You would need to make a wrapper with C headers and then link to the built C++.

Edit: Looks like you can use JoltC or even zphysics

4 Likes

…also alternatively, there’s a new kid in town, implemented in C and has a ‘native’ C API:

Haven’t tried it from Zig yet but’s it’s a pleasure to work with in C.

The feature set is a bit smaller than Jolt or Bullet though (e.g. no Cloth simulation)

5 Likes

translate-c only translates C code

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?

Sorry if these are dumb questions :sweat_smile:

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 :slight_smile:

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.

1 Like

Thanks both. Managed to get something working, you’re right that I needed add cpp-files.