#Path to dylib at runtime

1 messages · Page 1 of 1 (latest)

fickle granite
#

Hi! i'm quite new to such low-level languages like Zig and C, and i'm trying to get my setup working. I'am currently on MacOS
I want to use glfw lib in my project. But I also want to keep dylib directly in "dependencies" folder.

build.zig:

const std = @import("std");

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

    const exe = b.addExecutable(.{
        .name = "gyrocube",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.addLibraryPath(b.path("dependencies"));
    exe.linkSystemLibrary("glfw.3");
    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

So it compiles but executable doesn't know how to find dylib which is located in dependencies folder. I keep getting this error:

dyld[7109]: Library not loaded: /opt/homebrew/opt/glfw/lib/libglfw.3.dylib
Referenced from: <51D6ECF4-DFC8-38D4-8DD0-35DB566076A9> /Users/klyotskin/Desktop/gyrocube/zig-out/bin/gyrocube
Reason: tried: '/opt/homebrew/opt/glfw/lib/libglfw.3.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/glfw/lib/libglfw.3.dylib' (no such file), '/opt/homebrew/opt/glfw/lib/libglfw.3.dylib' (no such file)

Is there a way to tell the build system where dylib is located?
Thank you!

vapid hearth
#

Maybe exe.addIncludePath(.{ .path = "/opt/homebrew/lib" }); can solve your problem

#

Or the problem may be in library name, try changing it to just 'glfw'

#
// build.zig
const std = @import("std");

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

    const exe = b.addExecutable(.{
        .name = "gyrocube",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.addLibraryPath(.{ .path = "/opt/homebrew/lib" });

    exe.addIncludePath(.{ .path = "/opt/homebrew/include" });
    exe.linkSystemLibrary("glfw");
    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
// src/main.zig
const c = @cImport({
    @cInclude("GLFW/glfw3.h");
});

pub fn main () void {
    _ = c.glfwInit();
}

That code compiled on my machine

#

But if you still want to have library with headers in dependencies directory

#

Your code should work, I just added include paths

patent shell
#

also note that DLL loading on macOS is kinda "weird":

https://blog.krzyzanowskim.com/2018/12/05/rpath-what/

...at one point I knew how it worked, and how install_name_tool must be used on DLLs to modify the @rpath if needed, but I forgot all about it in the meantime (static linking FTW!). You can run otool -l [dll or exe] to see a list of all DLLs that an executable or DLL wants to load, and from what paths.