I’m trying to create a couple of “helloworld” projects (library + library user) to test my ability to use Zig package manager… and I invariably fail.
Could someone please take a look and tell me where do I go wrong?
I’m on 0.12.0-dev.3152+90c1a2c41
. Here’s what I tried:
- Created two folders for two projects:
test-lib-01
andtest-lib-user-01
. - In
test-lib-01
: ranzig init
. - In
test-lib-01
: inbuild.zig
commented out everything related to the executable part:
const exe = b.addExecutable(.{
.name = "test-lib-01",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
. . .
const exe_unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
. . .
test_step.dependOn(&run_exe_unit_tests.step);
- Edit: this is what was missing: in
test-lib-01
: inbuild.zig
added the following line
_ = b.addModule("test_lib_01",
.{
.root_source_file = .{ .path = "src/root.zig" },
.target = target, // Pass these values from the host project to the
.optimize = optimize, // dependency module
});
just before b.installArtifact(lib);
- In
test-lib-01
: removedsrc/main.zig
. - In
test-lib-01
: inbuild.zig.zon
, in.path
, uncommented the following lines:
"build.zig",
"build.zig.zon",
"src",
- In
test-lib-01
: ranzig build
command, observed the library binary filezig-out/lib/libtest-lib-01.a
. - In
test-lib-01
: removedzig-out
andzig-cache
folders. - Compressed
test-lib-01
folder astest-lib-01.tar.gz
and copied this file totest-lib-user-01
folder. - In
test-lib-user-01
: ranzig init
command. - In
test-lib-user-01
: inbuild.zig.zon
, in.dependencies
, added the following lines:
.test_lib_01 = .{
.url = "file:///home/archie/projects/zig-playground/test-lib-user-01/test-lib-01.tar.gz",
.hash = "1220d8cb8a71180f18834eb687ddb04b0500a78295d819cef6c89f6e5ff9eb417188",
},
Note the underscores in test_lib_01
.
- In
test-lib-user-01
: inbuild.zig
, after the call toconst exe = b.addExecutable(
and before the call tob.installArtifact(exe);
, added the following lines:
const zsp = b.dependency("test_lib_01",
.{
.target = target,
.optimize = optimize,
.openssl = false, // set to true to enable TLS support
});
exe.root_module.addImport("test_lib_01", zsp.module("test_lib_01"));
- In
test-lib-user-01
: ranzig build
command. Received the following error:
$ zig build
error: invalid option: -Dopenssl
/home/archie/.night.zig/zig-linux-x86_64-0.12.0-dev.3152+90c1a2c41/lib/std/Build.zig:1873:35: 0x1146229 in dependency__anon_16348 (build)
return dependencyInner(b, name, pkg.build_root, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, pkg.deps, args);
^
/home/archie/projects/zig-playground/test-lib-user-01/build.zig:40:29: 0x10fc3bc in build (build)
const zsp = b.dependency("test_lib_01",
^
/home/archie/.night.zig/zig-linux-x86_64-0.12.0-dev.3152+90c1a2c41/lib/std/Build.zig:1992:33: 0x10d69f3 in runBuild__anon_8909 (build)
.Void => build_zig.build(b),
^
. . .
error: the following build command crashed:
/home/archie/projects/zig-playground/test-lib-user-01/zig-cache/o/29cfa6b5f14d921c0a2e559bb6106d09/build /home/archie/.night.zig/zig-linux-x86_64-0.12.0-dev.3152+90c1a2c41/zig /home/archie/projects/zig-playground/test-lib-user-01 /home/archie/projects/zig-playground/test-lib-user-01/zig-cache /home/archie/.cache/zig --seed 0x1b272975 -Zc8310a48dd5b0fb0
Going with .openssl = false
produces the same error:
$ zig build
error: invalid option: -Dopenssl
/home/archie/.night.zig/zig-linux-x86_64-0.12.0-dev.3152+90c1a2c41/lib/std/Build.zig:1873:35: 0x1146229 in dependency__anon_16348 (build)
return dependencyInner(b, name, pkg.build_root, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, pkg.deps, args);
^
/home/archie/projects/zig-playground/test-lib-user-01/build.zig:40:29: 0x10fc3bc in build (build)
const zsp = b.dependency("test_lib_01",
^