Hi!
I’m new to zig and trying to figure out how exactly the build system works with modules. In my current project, I have the following directory structure:
.
├── build.zig
├── build.zig.zon
└── src
├── geo
│ ├── geo.zig
│ ├── ray.zig
│ └── vec3.zig
└── main.zig
Here are the relevant bits of the various files.
vec3.zig:
const std = @import("std");
pub const Vec3 = struct {
x: f64,
y: f64,
z: f64,
pub fn inv(self: Vec3) Vec3 {
return .{ .x = -self.x, .y = -self.y, .z = -self.z };
}
...
}
pub fn add(v1: Vec3, v2: Vec3) Vec3 {
return .{ .x = v1.x + v2.x, .y = v1.y + v2.y, .z = v1.z + v2.z };
}
geo.zig:
pub const Vec3 = @import("vec3.zig");
main.zig:
const std = @import("std");
const geo = @import("geo");
pub fn main() !void {
_ = geo.Vec3.Vec3{ .x = 1, .y = 3, .z = 4 };
}
build.zig:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const geo = b.addModule("geo", .{
.root_source_file = b.path("src/geo/geo.zig"),
.target = target,
});
...
const raytracer = b.addModule("raytracer", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "geo", .module = geo },
.{ .name = "color", .module = color },
},
});
const exe = b.addExecutable(.{
.name = "raytracer",
.root_module = raytracer,
});
const exe_check = b.addExecutable(.{
.name = "raytracer",
.root_module = raytracer,
});
b.installArtifact(exe);
...
}
What I’d like to do is import the geo module then access geo.Vec3{} and geo.Add() instead of having to call geo.Vec3.Vec3{}… essentially I want to “flatten” the module as if it was all in one file like how #include in C or the package system in Go. Alternatively, if I’m approaching this completely wrong and there is a more idiomatic way of handling modules, I’d love to find out!
I can’t seem to find any good resources on building zig module past the very basic examples. I also tried going through the std lib source code for examples, but I couldn’t quite figure out what the pattern was (seemingly @This is used somehow?).
Thank you in advance!