Building and linking to cmake projects in zig

so now i have this ^^^^^

im trying to use this lib:

zimq/build.zig at archive/cmake-build · uyha/zimq · GitHub maybe this can help you a bit, i abandoned this approach though since i can’t make it work to only rebuild when it is needed.

got this so far, any suggestions or guidance on what i should include next?

well, you can start by configuring and building the project with b.addSystemCommand, similar to how I did in the build script I shared. However, have you tried building it using just cmake yet? I mean not using build.zig, just build it using the cmake command in your system.

1 Like

why b. over gns_dep.

wut, not sure what you mean

why b.addSystemCommand over gns_dep.addSystemCommand?

because you can’t do that. b.addSystemCommand is for running a command, gns_dep.addSystemCommand simply does not exist.

it does if you do gns_dep.builder

I’m not sure how that’s different from just using b.addSystemCommand, but you do you.

To clarify, gns_dep.builder == b. There’s only one builder.

ok so far so good?

fn build_gns(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void // *std.Build.Module
{
    const gns_dep = b.dependency("gns", .{
        .target = target,
        .optimize = optimize,
    });

    const gns_src_path = gns_dep.path("");

    std.debug.print("gns_src_path: {s}\n", .{gns_src_path.getPath(b)});

    const cmake = b.findProgram(&.{"cmake"}, &.{}) catch @panic("CMake not found");

    const cmake_build_type = switch (optimize) {
        .Debug => "-DCMAKE_BUILD_TYPE=Debug",
        .ReleaseSmall => "-DCMAKE_BUILD_TYPE=MinSizeRel",
        else => "-DCMAKE_BUILD_TYPE=Release",
    };

    const cmake_configure = b.addSystemCommand(&.{
        cmake,
        "-DBUILD_SHARED_LIBS=OFF",
        cmake_build_type,
        "-S",
    });
    cmake_configure.addDirectoryArg(gns_src_path);
    cmake_configure.addArg("-B");
    const gns_build_dir = cmake_configure.addOutputDirectoryArg("gns_build");

    const cmake_build = b.addSystemCommand(&.{ cmake, "--build" });
    cmake_build.addDirectoryArg(gns_build_dir);
}

at this point, running zig build should create the compiled library, do you see that?

well i have this now, is the build directory correct? how do i link it now?

well, at this point you can do something like pong.addObjectFile(gns_build_dir.path("libname.a")), then in your Zig file, try @cImport the header files and build it.