How to import and use another file outside of main?

New to Zig here. I’m using zig 0.13. I’m wrestling with the build system, and how to import another file I wrote.

main.zig:

const std = @import("std");
const prolly = @import("prolly");
const testing = std.testing;

pub fn main() !void {
    std.debug.print("Hello {s}!\n", .{"world"});
    prolly.BranchNode(u32).init();
}

I have another zig file I wrote outside of main under “src/unicit/prolly.zig”

I started with the standard generated zig file.

build.zig:

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

    //********** Modules *********

    const prolly_mod = b.createModule(.{ .root_source_file = b.path("src/unicit/prolly") });

    //************* Static Library ************

    const lib = b.addStaticLibrary(.{
        .name = "database",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    lib.root_module.addModule("prolly", prolly_mod);

    b.installArtifact(lib);

    //************* Executable ************

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

    exe.root_module.addImport("prolly", prolly_mod);

    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);

    run_cmd.step.dependOn(b.getInstallStep());

    .... rest of file
}

when I run zig build run, I get:

run
└─ run database
   └─ install
      └─ install database
         └─ zig build-lib database Debug native 1 errors
src/main.zig:2:24: error: no module named 'prolly' available within module root
const prolly = @import("prolly");
                       ^~~~~~~~
referenced by:
    main: src/main.zig:16:5
    callMain: /Users/iamwil/.zvm/0.13.0/lib/std/start.zig:524:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/Users/iamwil/.zvm/0.13.0/zig build-lib -ODebug -Mroot=/Users/iamwil/projects/code/fruitful_town/database/src/main.zig --cache-dir /Users/iamwil/projects/code/fruitful_town/database/.zig-cache --global-cache-dir /Users/iamwil/.cache/zig --name database -static --listen=- 
run
└─ run database
   └─ zig build-exe database Debug native 1 errors
src/main.zig:2:24: error: no module named 'prolly' available within module root
const prolly = @import("prolly");
                       ^~~~~~~~
referenced by:
    main: src/main.zig:16:5
    callMain: /Users/iamwil/.zvm/0.13.0/lib/std/start.zig:524:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/Users/iamwil/.zvm/0.13.0/zig build-exe -ODebug -Mroot=/Users/iamwil/projects/code/fruitful_town/database/src/main.zig --cache-dir /Users/iamwil/projects/code/fruitful_town/database/.zig-cache --global-cache-dir /Users/iamwil/.cache/zig --name database --listen=- 
Build Summary: 0/7 steps succeeded; 2 failed (disable with --summary none)
run transitive failure
└─ run database transitive failure
   ├─ zig build-exe database Debug native 1 errors
   └─ install transitive failure
      ├─ install database transitive failure
      │  └─ zig build-lib database Debug native 1 errors
      └─ install database transitive failure
         └─ zig build-exe database Debug native (reused)
error: the following build command failed with exit code 1:
/Users/iamwil/projects/code/fruitful_town/database/.zig-cache/o/cade683869b9cd1552d6a8a8dc66441d/build /Users/iamwil/.zvm/0.13.0/zig /Users/iamwil/projects/code/fruitful_town/database /Users/iamwil/projects/code/fruitful_town/database/.zig-cache /Users/iamwil/.cache/zig --seed 0x5132ac68 -Zfac4b901eb6902e8 run

I’m a bit stuck. I looked at the docs, and the references, but I can’t piece together what I’m supposed to do in order to import and use another zig file that I wrote outside of main.zig. Thanks.

Thats’s not what you wrote previously.

This is correct.

2 Likes

According to the compiler error you have not written @import("prolly") but instead @import("unicit/prolly").

@import("prolly") would be correct because that is the name of the import you added in the build file.

1 Like

Sorry for the typo. I’ve updated the response after zig build run. I’m still getting the same thing.

From that, I take it the name of the import is just the name of the module in the build.zig? With different ecosystems, it’s not clear whether the import path is relative to the current file or relative to the source root.

I’ve updated it, but it still can’t find it. I’ve also make sure that the directory exists and is spelled correctly.

➜  database git:(main) ✗ ls src/unicit 
prolly.zig

What else should I check?

const prolly_mod = b.createModule(.{ .root_source_file = b.path("src/unicit/prolly.zig") });

Your zig source files should have .zig extension.

1 Like

Ah, thanks for the typo catch.

A last thing I had to do was use addImport, rather than addModule on root_module.

    lib.root_module.addImport("prolly", prolly_mod);

It compiles! thanks all!

3 Likes

Just as a follow-up. I found I didn’t need the module at all.

I just needed to use the file extension, and it’ll include the file, but only as long as it’s in a subdirectory.

const prolly = @import("unicit/prolly.zig");