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.