addTranslateC for system headers

I’m trying to switch the following to the build system:

const dbus = @cImport({
    @cInclude("dbus/dbus.h");
}):

Since this is going away, I want to figure out how to use the build system’s b.addTranslateC() to do this instead.

const dbus_headers = b.addTranslateC(.{
    .root_source_file = ?WHAT GOES HERE?,
    .target = target,
    .optimize = optimize,
});
exe_mod.addImport("dbus", dbus_headers.createModulu());

I’ve gotten this to work when I pull in a c dependency using zig fetch because I can get a LazyPath from the dependency. But since the b.path() is relative to the build, how would I do that for a system library in /usr/include. I could hard code it, but is there a better way?

1 Like

It is actually contained in my variant of a cursed entry (the whole thing is cursed, not that part specifically) What's the most cursed Zig you can write? - #16 by Sze :

You also could use an actual .c or .h file that contains similar code, I originally saw somebody turn it into a single expr .root_source_file = b.addWriteFiles().add(...) that also works, but I don’t remember where I saw it.

1 Like

That helps with how to get the #include <dbus/dbus.h> in there, but I ran into the following error.

error: ‘dbus/dbus.h’ file not found

It looks like the addTranslateC step does not have any include paths by default, it’s not picking up the system include path. In fact, adding debug printing on the include paths doesn’t print any paths:

    const dbus_headers = b.addTranslateC(.{
        .root_source_file = b.path("include/dbus.h"),
        .target = target,
        .optimize = optimize,
    });
    std.debug.print("C INCLUDE PATHS\n", .{});
    for (dbus_headers.include_dirs.items) |dir| {
        std.debug.print("Include Dir: {s}\n", .{dir.path.getDisplayName()});
    }

/v/h/s/g/break-reminder$ zig build
C INCLUDE PATHS
install
└─ install windchime
└─ zig build-exe windchime Debug native
└─ translate-c 1 errors
/var/home/southporter/git/windchime/include/dbus.h:1:10: error: ‘dbus/dbus.h’ file not found
#include <dbus/dbus.h>

Is there some way to get the Build’s search directories and add those here?

Adding .link_libc = true, might fix it.
see: translate-c build step lacks a way to link system libraries · Issue #20649 · ziglang/zig · GitHub

I tried that and it reports the same error. I’ll keep looking at the API.