Translate-C error on 0.17.0-dev.607+456b2ec07 when importing GTK4 libraries

Hello all,

I recently started a new project to rewrite one of my existing C#/.NET/WPF projects into Zig + GTK 4 (project will be 2-BSD licensed so dynamically linking GTK-4 is a requirement since I don’t want it to become LGPL). I’ve written/released and currently maintain a few other Zig projects, but this will be my first GUI project in Zig and also the first one that I’m starting to link with external C libraries. I ran into the same issue that was described here since I first started the project on 0.16.0, after looking at the comments, and the status of the tickets in the upstream translate-c and arocc projects, it seemed like everything was already closed/resolved/merged, so I thought maybe I need to update to latest master for now and then I can chill on 0.17.0 once that gets officially released. However, even on 0.17.0-dev.607+456b2ec07, I get the same issue. So I wanted to confirm if it’s just me wiring things incorrectly, or if the changes aren’t in the latest master yet. I’m also fine downgrading to 0.16.0, and importing translate-c as an external dependency if that makes things any easier since I normally like to track the latest stable release, and upgrade to the next stable when I get a chance. But if that’s too difficult I’m fine with tracking master for this project and stabilizing on 0.17.0 for now. Thank you!

I’m running all of this on FreeBSD 15.0-RELEASE. pkgconf-2.4.3,1 and gtk4-4.20.3 are already installed on the host and Zig is detecting them correctly. Without pkgconf being installed, I was getting an issue about gtk/gtk.h not being found even though the linkSystemLibrary("gtk-4", .{}); call didn’t fail and found the correct library on FreeBSD. Once I switched to pkgconf, and changed it from “gtk-4” to “gtk4”, then it found the rest of the stuff and that’s when I started getting the multi pragma failure.

zig version

jon@leslie:~/Projects/App $ zig version
0.17.0-dev.607+456b2ec07

error when attempting to import gtk/gtk.h

jon@leslie:~/Projects/App $ zig build
install
└─ install App
   └─ compile exe App Debug native
      └─ translate-c 2 errors
error: translation failure
/usr/local/include/gtk-4.0/gdk/version/gdkversionmacros.h:19:2: error: \"Only <gdk/gdk.h> can be included directly.\"
#error "Only <gdk/gdk.h> can be included directly."
 ^
error: 2 compilation errors
failed command: /home/jon/.zig/zig translate-c -lc --cache-dir .zig-cache --global-cache-dir /home/jon/.cache/zig -I/usr/local/include/gtk-4.0 -I/usr/local/include/pango-1.0 -I/usr/local/include/fribidi -I/usr/local/include -I/usr/local/include/harfbuzz -I/usr/local/include/gdk-pixbuf-2.0 -I/usr/local/include/cairo -I/usr/local/include/freetype2 -I/usr/local/include/libpng16 -D_THREAD_SAFE -I/usr/local/include/pixman-1 -I/usr/local/include/graphene-1.0 -I/usr/local/lib/graphene-1.0/include -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include -L/usr/local/lib -lgtk-4 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lvulkan -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl /home/jon/Projects/App/src/c.h --listen=-

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

error: the following maker command exited with code 1:
/home/jon/.cache/zig/o/201b413da3e9f6d025320505604c88e4/maker --zig /home/jon/.zig/zig --zig-lib-dir /home/jon/.zig/lib --build-root /home/jon/Projects/App --local-cache .zig-cache --global-cache /home/jon/.cache/zig --configuration .zig-cache/c/1efa3748f8b1e0f0168dbc146a0def18 --seed 0xfd57a27f

build.zig

...

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // Translate GTK / C Dependencies
    const translate_c = b.addTranslateC(.{
        .root_source_file = b.path("src/c.h"),
        .target = target,
        .optimize = optimize,
    });
    translate_c.linkSystemLibrary("gtk4", .{});

    const mod = b.addModule("App", .{
        .root_source_file = b.path("src/root.zig"),
        .target = target,
    });

    const exe = b.addExecutable(.{
        .name = "App",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "App", .module = mod },
                .{ .name = "c", .module = translate_c.createModule() },
            },
        }),
    });
 }

...

src/c.h

#include <gtk/gtk.h>