#Linking libraries and zig build run

1 messages · Page 1 of 1 (latest)

calm sundial
#

Hey all, I'm new to zig and I'm trying to set up a build.zig that links to raylib, builds with zig build, and runs with zig build run. I'm able to link to raylib, build, and run my main.exe manually but if I try run zig build run it fails. See the bottom of this post for the failure.

Context

I'm using version 0.12.0-dev.

My folder structure is:

external/
├─ include/
│  ├─ some raylib headers
├─ libraries/
│  ├─ librarylib.a
│  ├─ libraylibdll.a
│  ├─ raylib.dll
src/
├─ main.zig
build.zig

The bulk of my build script:

const exe = b.addExecutable(.{ .name = "main", .root_source_file = .{ .path = "src/main.zig" }});
exe.addLibraryPath(.{ .path = "./external/libraries"});
exe.linkSystemLibrary2("raylibdll", .{});
exe.addIncludePath(.{ .path = "./external/include" });
const installArtifact = b.addInstallArtifact(exe,  .{});
// Make build step depend on installing the exe
b.getInstallStep().dependOn(&installArtifact.step);

// Copy the .dll from the external folder into the build/bin folder so that main.exe can find it
const rayCopyStep = b.addInstallFile(.{.path = "./external/libraries/raylib.dll"}, "./bin/raylib.dll");

// Make it so build step depends on the copy
b.getInstallStep().dependOn(&rayCopyStep.step);

// Create the "zig build run" step
const run_exe = b.addRunArtifact(exe);
const run_step = b.step("run", "Run app");
run_step.dependOn(&run_exe.step);

zig build works as expected and I can run the produced main.exe.

Problem

When I run zig build run I get

run main: error: the following command exited with error code 53:
D:\...\zig-cache\o\0a5c467306bba98e6cccb426256ec040\main.exe

If I run that main.exe from zig-cache it fails because it can't find the raylib dll which makes sense because it's not copied into that folder. So I know the issue is that raylib isn't copied into zig-cache so main.exe can't run. How can I fix that? Can I force zig build run to run the exe in zig-out/bin?

#

I'll post in here with things I've tried as they come up.

I've tried to make the run_step depend on the rayCopyStep, but that doesn't copy the dll into the zig-cache folder.

Edit: Full error for more context - not sure what build.exe is

run main: error: the following command exited with error code 53:
D:\Projects\RayZigSim\zig-cache\o\0a5c467306bba98e6cccb426256ec040\main.exe
Build Summary: 1/3 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run main failure
error: the following build command failed with exit code 1:
D:\Projects\RayZigSim\zig-cache\o\13483b6a84aa096ec80273190bedc87f\build.exe C:\Program Files\Zig\zig.exe D:\Projects\RayZigSim D:\Projects\RayZigSim\zig-cache C:\Users\m_d_e\AppData\Local\zig --seed 0x8e862e7f run   
silver mesa
#

Can I force zig build run to run the exe in zig-out/bin
there is a line in the generated build.zig by zig init-exe for this:

const run_cmd = b.addRunArtifact(exe);

 // By making the run step depend on the install step, it will be run from the
 // installation directory rather than directly from within the cache directory.
 // This is not necessary, however, if the application depends on other installed
 // files, this ensures they will be present and in the expected location.
 run_cmd.step.dependOn(b.getInstallStep());

I don't know the solution to your problem but maybe this would help