#How to link dylib library in zig?

1 messages · Page 1 of 1 (latest)

azure fractal
#

I did

exe.addLibraryPath(b.path("external/"));

I have libBinding.dylib, how to actually link it?

tardy spade
#

Is the type of exe *Step.Compile? If so, its addLibraryPath method has been deprecated.

azure fractal
#

@tardy spade I have this build.zig currently

const std = @import("std");
pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const mod = b.addModule("zig_metal", .{
        .root_source_file = b.path("src/root.zig"),
        .target = target,
    });
    const exe = b.addExecutable(.{
        .name = "zig_metal",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "zig_metal", .module = mod },
            },
        }),
    });
    exe.addLibraryPath(b.path("external/"));
    b.installArtifact(exe);
    const run_step = b.step("run", "Run the app");
    const run_cmd = b.addRunArtifact(exe);
    run_step.dependOn(&run_cmd.step);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }
    const mod_tests = b.addTest(.{
        .root_module = mod,
    });
    const run_mod_tests = b.addRunArtifact(mod_tests);
    const exe_tests = b.addTest(.{
        .root_module = exe.root_module,
    });
    const run_exe_tests = b.addRunArtifact(exe_tests);
    const test_step = b.step("test", "Run tests");
    test_step.dependOn(&run_mod_tests.step);
    test_step.dependOn(&run_exe_tests.step);
}
tardy spade
azure fractal
#

So what should I do?

#

I need to add in the root_module?

#

instead of calling these functions/

tardy spade
#
exe.root_module.addLibraryPath(b.path("external/"));
exe.root_module.linkSystemLibrary("Binding", .{});

Does it work?

azure fractal
#

@tardy spade
build.zig:21:20: error: member function expected 1 argument(s), found 2
exe.root_module.linkLibrary("MetalLibrary", .{});

tardy spade
#

exe.root_module.linkSystemLibrary

azure fractal
#

Its not system library

#

Its the custom library I built in c

#

let me give it a try

#

Yeah its searching for system library

tardy spade
#

addObjectFile and use the full path

#

The behavior of linkSystemLibrary is similar to adding -lxxx in compiler, where xxx is the name of the library. Generally, as long as you add the LibraryPath, it can be found. However, this assumes that the library name starts with lib.

azure fractal
#

No @tardy spade it's not working either
thread 174294 panic: sub_path is expected to be relative to the build root, but was this absolute path: '/Users/ashu2427/Coding/zig-projects/zig-metal/external/libMetalBridge.dylib'. It is best avoid absolute paths, but if you must, it is supported by LazyPath.cwd_relative