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

2 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)

3 Likes