Scenario
I’m bulding a library, and includes examples.
I already implemented a build.zig file that compiles examples with zig build examples.
main part be like:
const example_step = b.step("examples", "Build all examples");
const examples = [_][]const u8{
"foo",
};
for (examples) |name| {
// executable
const source = b.fmt("examples/{s}.zig", .{name});
const exe = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.root_source_file = b.path(source),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "mylib", .module = mod },
},
}),
});
// install
const install = b.addInstallArtifact(exe, .{
.dest_dir = .{ .override = .{ .custom = "examples" } },
});
example_step.dependOn(&install.step);
}
Problems
- working with ZLS, it doesn’t recognize the
mylibimport in examples (not giving completions for stuff in the library).
I’m thinking maybe somehow redirecting the import to../src/root.zigin zls config?
Looked in the docs but didn’t find anything similar - I originally made
build.zigscan for files underexamplesautomatically, but it seems to mess up build caching (failing to compile new files and ignoring removed ones)
So eventually ended up with the examples list approach above. It’d be nice to make the scan work.