Hello, I am currently working on a simple game and want to build the game as a DLL and then have the executable depend on that. One of the advantages of this setup is the possibility of hot reloading the game code as shown in the HandmadeHero series.
This works, but I have an issue that the executable won't find the game DLL when building from a clean slate initially, meaning zig-out is not created and there's no DLL in there. So the first time it get an error that it can't find the game DLL, but on the second time building it succeds.
Is there a way that I can ensure that the lib DLL is built before the game() function for adding the DLL to the EXE is ran? I though maybe dependOn would do this but it doesn't help it seems.
I have the following build.zig code, theres more to it like the functions sdl2, cimgui etc but they are just doing the necessary stuff for dependencies to work.
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const build_mode = b.option(BuildMode, "build_mode", "dynamic_exe or hotreload") orelse .dynamic_exe;
const build_exe = (build_mode == .dynamic_exe);
const build_lib = (build_mode == .hotreload or build_mode == .dynamic_exe);
const hotreload = build_lib;
var options = b.addOptions();
options.addOption(bool, "hotreload", hotreload);
options.addOption(bool, "gl_debug", b.option(bool, "gl_debug", "Enable OpenGL debug checking") orelse false);
const lib = b.addSharedLibrary(.{
.name = "game",
.root_source_file = .{ .path = "src/game.zig" },
.target = target,
.optimize = optimize,
});
lib.addOptions("build_options", options);
glad(lib);
stbImage(lib);
lib.linkLibC();
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "marsh",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.step.dependOn(&lib.step);
// Dependencies
glad(exe);
stbImage(exe);
sdl2(exe, b);
cimgui(exe);
game(exe, b);
exe.linkLibCpp();
exe.addOptions("build_options", options);
if (build_exe) {
b.installArtifact(exe);
}
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
tests(b, target, optimize);
}