How to produce more than one executable in a single build.zig?

I’ve just updated my client-server pair example.
Currently I am building them just by

/opt/zig-0.14/zig build-exe client.zig
/opt/zig-0.14/zig build-exe server.zig

Now I would like to use build.zig and want to have one build.zig for building the pair at once. Is there a way to do so?

If the question has already been asked/answered, please point me to a relevant topic.

Yes, this should work with zig build:

const client_exe = b.addExecutable(.{
    .name = "client",
    .target = target,
    .optimize = optimize,
    .root_source_file = b.path("client.zig"),
});
b.installArtifact(client_exe);

const server_exe = b.addExecutable(.{
    .name = "server",
    .target = target,
    .optimize = optimize,
    .root_source_file = b.path("server.zig"),
});
b.installArtifact(server_exe);
5 Likes

yes, it’s working as needed, thanks!
will mark your answer as solution.

1 Like

Variant with for loop:

const std = @import("std");

pub fn build(b: *std.Build) void {

    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const apps = [_][]const u8{"app1", "app2", "app3"};

    inline for (apps) |app| {
        const exe = b.addExecutable(.{
            .name = app,
            .root_source_file = b.path("src/" ++ app ++ ".zig"),
            .target = target,
            .optimize = optimize,
            .single_threaded = true,
            .strip = true,
        });
        b.installArtifact(exe);
    }
}
2 Likes