If I don’t specify target in my module declaration, I get an error:
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// public exports
// argparser
const argparser_module = b.addModule("argparser", .{
.root_source_file = b.path("src/argparser/root.zig"),
});
// zig build test-argparser
const step_test_argparser_module = b.step("test-argparser", "run unit tests for argparser module");
const argparser_module_unit_tests = b.addTest(.{ .root_module = argparser_module });
const run_argparser_module_unit_tests = b.addRunArtifact(argparser_module_unit_tests);
step_test_argparser_module.dependOn(&run_argparser_module_unit_tests.step);
jeff@jeff-debian:~/repos/gatorcat$ zig build test-argparser
thread 179492 panic: the root Module of a Compile step must be created with a known 'target' field
/home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib/std/Build/Step/Compile.zig:387:9: 0x1582016 in create (std.zig)
@panic("the root Module of a Compile step must be created with a known 'target' field");
^
/home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib/std/Build.zig:882:19: 0x16ca128 in addTest (std.zig)
return .create(b, .{
^
/home/jeff/repos/gatorcat/build.zig:23:50: 0x157f52b in build (build.zig)
const argparser_module_unit_tests = b.addTest(.{ .root_module = argparser_module });
^
/home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib/std/Build.zig:2264:33: 0x157daba in runBuild__anon_34149 (std.zig)
.void => build_zig.build(b),
^
/home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib/compiler/build_runner.zig:463:29: 0x128e524 in main (build_runner.zig)
try builder.runBuild(root);
^
/home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib/std/start.zig:699:88: 0x1294040 in callMain (std.zig)
if (fn_info.params[0].type.? == std.process.Init.Minimal) return wrapMain(root.main(.{
^
/home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib/std/start.zig:190:5: 0x1278201 in _start (std.zig)
asm volatile (switch (native_arch) {
^
error: the following build command terminated with signal ABRT:
.zig-cache/o/8591f77b64d2c25af47ae4462cd89120/build /home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/zig /home/jeff/.config/Code/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib /home/jeff/repos/gatorcat .zig-cache /home/jeff/.cache/zig --seed 0x91912ac4 -Z6a263f3ee24db1e3 test-argparser
So I have to specify target like this:
const argparser_module = b.addModule("argparser", .{
.root_source_file = b.path("src/argparser/root.zig"),
.target = target,
});
- Why is target required?
- Why is target required but not optimize?