Translate-C help

For funzies and practice I’m trying to make a binding for raysan’s rini.h single header project.
Link: https://github.com/raysan5/rini

However I’m constantly running into compile errors.

C:\zig\zig versions\zig-x86_64-windows-0.16.0\lib\std\Io\Writer.zig:1077:32: error: no field or member function named 'format' in 'Build.LazyPath'
            'f' => return value.format(w),
                          ~~~~~^~~~~~~
C:\zig\zig versions\zig-x86_64-windows-0.16.0\lib\std\Build.zig:2336:22: note: union declared here
pub const LazyPath = union(enum) {
                     ^~~~~
referenced by:
    print__anon_159585: C:\zig\zig versions\zig-x86_64-windows-0.16.0\lib\std\Io\Writer.zig:719:25
    allocPrint__anon_159579: C:\zig\zig versions\zig-x86_64-windows-0.16.0\lib\std\fmt.zig:633:20
    10 reference(s) hidden; use '-freference-trace=12' to see all references
zig-pkg\aro-0.0.0-JSD1Qum6OgB83hmRWguTl2Z9x3rEjHWIu8f_RsMXJTTg\build.zig:248:16: error: no field or member function named 'addPassthruArgs' in 'Build.Step.Run'
        run_cmd.addPassthruArgs();
        ~~~~~~~^~~~~~~~~~~~~~~~
C:\zig\zig versions\zig-x86_64-windows-0.16.0\lib\std\Build\Step\Run.zig:1:1: note: struct declared here
const Run = @This();
^~~~~
zig-pkg\aro-0.0.0-JSD1Qum6OgB83hmRWguTl2Z9x3rEjHWIu8f_RsMXJTTg\build.zig:248:16: note: method invocation only supports up to one level of implicit pointer dereferencing
zig-pkg\aro-0.0.0-JSD1Qum6OgB83hmRWguTl2Z9x3rEjHWIu8f_RsMXJTTg\build.zig:248:16: note: use '.*' to dereference pointer
zig-pkg\translate_c-0.0.0-Q_BUWuEVBwCbZUsNorTQ6-d3nYx8KDgHHWtVWBzgzyB5\build.zig:77:16: error: no field or member function named 'addPassthruArgs' in 'Build.Step.Run'
        run_cmd.addPassthruArgs();
        ~~~~~~~^~~~~~~~~~~~~~~~
C:\zig\zig versions\zig-x86_64-windows-0.16.0\lib\std\Build\Step\Run.zig:1:1: note: struct declared here
const Run = @This();
^~~~~
zig-pkg\translate_c-0.0.0-Q_BUWuEVBwCbZUsNorTQ6-d3nYx8KDgHHWtVWBzgzyB5\build.zig:77:16: note: method invocation only supports up to one level of implicit pointer dereferencing
zig-pkg\translate_c-0.0.0-Q_BUWuEVBwCbZUsNorTQ6-d3nYx8KDgHHWtVWBzgzyB5\build.zig:77:16: note: use '.*' to dereference pointer
zig-pkg\translate_c-0.0.0-Q_BUWuEVBwCbZUsNorTQ6-d3nYx8KDgHHWtVWBzgzyB5\build\Translator.zig:134:48: error: union 'Build.LazyPath' has no member named 'zig_lib'
    run.addPrefixedDirectoryArg("--zig-lib=", .zig_lib);
                                              ~^~~~~~~
C:\zig\zig versions\zig-x86_64-windows-0.16.0\lib\std\Build.zig:2336:22: note: union declared here
pub const LazyPath = union(enum) {
                     ^~~~~

I also tried to clone the Translate-C repo and build the examples there but I get the same error.

Here’s my build.zig:

const std = @import("std");

const Translator = @import("translate_c").Translator;

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

    // Prepare the `translate-c` dependency.
    const translate_c = b.dependency("translate_c", .{});

    const header: Translator = .init(translate_c, .{
        .c_source_file = b.path("src/rini.h"),
        .target = target,
        .optimize = optimize,
    });
    header.run.addArg("-Wno-pramga-once-outside-header");

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

    exe.root_module.addIncludePath(b.path("src"));

    b.installArtifact(exe);
}

I’m guessing the Translate-C repo is broken, but I don’t see that others have any problems with compilation. (I’m using zig 0.16.0 and tried the main and the 0.16.x branch too).

How should I tackle this problem? Or to say, how others make bindings for single header projects or C projects?

The error you showed are related to the build system rework, which landed a few days ago. The version of Translate-C you’re using has already migrated to it, but you’re still using zig 0.16. the 0.16 version of Translate-C should work (or you could switch to main zig).

1 Like

If I switch to the branch zig-0.16.x and use zig fetch --save git+https://codeberg.org/ziglang/translate-c#zig-0.16.x it still shows me the same error.

If I check the .zig.zon file it shows me the minimum version for 0.17, but I fetched the 0.16?

Edit:
Deleted the zig.zon and refetched. The minimum version now is 0.16 but I still have the same error.

I don’t see how that’s possible, given that Translate-C’s build.zig for 0.16 doesn’t have addPassthruArgs anywhere. You should check inside zig-pkg if the version of Translate-C is the correct one. Maybe the command you used is fetching main instead ot the branch
But in any case, I looked at the header file you pointed and I don’t see why you’d want to fetch Translate-C. The version that is embedded in Zig should be sufficient to translate it. You shouldn’t be translating the implementation.

1 Like