You can use zig fetch
to conveniently set the hash in the build.zig.zon
file and update an existing dependency.
Run the following command to fetch the zBench package:
zig fetch https://github.com/hendriknielaender/zbench/archive/<COMMIT>.tar.gz --save
Using zig fetch
simplifies managing dependencies by automatically handling the package hash, ensuring your build.zig.zon
file is up to date.
-
Declare zBench as a dependency in
build.zig.zon
:.{ .name = "my-project", .version = "1.0.0", .paths = .{""}, .dependencies = .{ + .zbench = .{ + .url = "https://github.com/hendriknielaender/zbench/archive/<COMMIT>.tar.gz", + }, }, }
-
Add the module in
build.zig
:const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const opts = .{ .target = target, .optimize = optimize }; + const zbench_module = b.dependency("zbench", opts).module("zbench"); const exe = b.addExecutable(.{ .name = "test", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); + exe.root_module.addImport("zbench", zbench_module); exe.install(); ... }
-
Get the package hash:
$ zig build my-project/build.zig.zon:6:20: error: url field is missing corresponding hash field .url = "https://github.com/hendriknielaender/zbench/archive/<COMMIT>.tar.gz", ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ note: expected .hash = "<HASH>",
-
Update
build.zig.zon
package hash value:.{ .name = "my-project", .version = "1.0.0", .paths = .{""}, .dependencies = .{ .zbench = .{ .url = "https://github.com/hendriknielaender/zbench/archive/<COMMIT>.tar.gz", + .hash = "<HASH>", }, }, }
On your project root directory make a directory name libs.
- Run
git submodule add https://github.com/hendriknielaender/zBench libs/zbench
- Then add the module into your
build.zig
exe.root_module.addAnonymousImport("zbench", .{
.root_source_file = b.path("libs/zbench/zbench.zig"),
});
Now you can import like this:
const zbench = @import("zbench");
For more information on the Zig build system and useful tricks, check out these resources: