Importing zig dependencies

I’m trying to wrap my head around exactly how to add repos as a dependency in the build system. Currently I have two dummy repos SimpleLib and SimpleExe. I’ve poked and prodded other recent examples and created the current build in SimpleExe as best as I can understand it, however the closest I’ve gotten to a successful build is error: file exists in multiple modules.

At the most basic level, how do I add a dependency from repo A and include it in a file in repo B?

Hello @cohors, welcome to ziggit :slight_smile:

You can import the module from the simple_lib package in exe.

    const simple_module = b.dependency("simple_lib", .{
        .target = target,
        .optimize = optimize,
    }).module("SimpleLib"));

    const exe = b.addExecutable(.{
        .name = "examples",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("SimpleLib", simple_module);
    b.installArtifact(exe);
1 Like

That looks like a cleaner solution than what I saw and it builds. Follow up question, build works but zig test src/main.zig fails for module not found. Is there somewhere else I need to import to for tests?

zig test doesn’t use the build system, you have to use zig build test and have a corresponding test step in your build.zig. The sample build.zig created by zig init has such a test step. To that test step you add imports just like you do for the exe.

2 Likes

got it, thanks.

1 Like