Example packaging a library

Could you please elaborate a little bit on the zig-router example?
I’m a bit confused by the naming.

  1. The project on Github is named zig-router (with a dash)
  2. zig-router’s readme states that you
    a) must add a dependency to your build.zig.zon and name it .zig_router (also with a dash), and
    b) add the following lines to your build.zig:
const zig_router = b.dependency("zig_router", .{}).module("zig-router");
exe.root_module.addImport("zig-router", zig_router);

Please note that you pass zig_router (with underscore) to b.dependency, and then zig-router to module and to addImport.

I would love to learn about these names. What do they represent - git project name, library name - is it libzig_router.a or libzig-router.a?
What is a module in this context?

For example, if I’m writing a static library and want it to be usable via Zig package manager, do I use b.addStaticLibrary in the build function, b.addModule, or both?

What is the use / meaning of name in addModule(b: *Build, name: []const u8 ... and in options.name passed to addStaticLibrary(b: *Build, options: StaticLibraryOptions)? Do they have to be the same?

This is so confusing…

When I’ve viewed from the beginning of ShowCase list, the zig-router is found as simple case.

IMO, I prefer to use following rule:

  • project name: Kebab case (e.g. zig-router)
    • Conventionally, widely used in github
  • package name: Snake case (e.g. zig_router)
    *A field name of the .zon format can not use Kebab case.
  • module (import) name: trimming prefix (e.g. router)
    • the prefix is redundant

Currently, a package naming rule does not indicate officially.
I would like to gather opinions.

Okay, let me rephrase the whole thing -
I wanted to create a simple library project (which builds perfectly fine with zig build), accessible via Zig package manager, and a simple project using this library (i call it zsp-user-01):

.dependencies =
.{
    .zigStructPrint =
    .{
         .url = "https://github.com/Durobot/zigStructPrint/archive/9ced743d434527147a9a61cf11c3db33201f24a3.tar.gz",
         .hash = "1220ca283c7e4240adbf9dfa31f7894818efc93ce54300521dad777a2670e9674dd8",
    }
},

and in build.zig:

    // --- in your build.zig's build function, add the following before b.installArtifact(exe): ---
    const zsp = b.dependency("zigStructPrint",
    .{
        .target = target,
        .optimize = optimize,
        .openssl = false, // set to true to enable TLS support
    });

    exe.root_module.addImport("zigStructPrint", zsp.module("zigStructPrint"));
    // --- ---

But I’m getting this error when I try to zig build zsp-user-01:

$ zig build
error: invalid option: -Dopenssl
/home/archie/apps/zig/lib/std/Build.zig:1873:35: 0x1143249 in dependency__anon_16347 (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/zsp-user-01/build.zig:43:29: 0x10fa2aa in build (build)
    const zsp = b.dependency("zigStructPrint",
                            ^
/home/archie/apps/zig/lib/std/Build.zig:1992:33: 0x10d60e3 in runBuild__anon_8908 (build)
        .Void => build_zig.build(b),
                                ^

Could it be because I don’t have call b.addModule in zigStructPrint’s build.zig?

I have tried adding this call to zigStructPrint’s fn build, just before calling b.addStaticLibrary:

    _ = b.addModule("zigStructPrint", // package_name
    .{
        .root_source_file = .{ .path = "src/root.zig" },
        .target = target,
        .optimize = optimize,
    });

And, since I didn’t want to push this change to github without testing it, I have downloaded zigStructPrint’s tarball, replaced build.zig within with the modified version, replaced .url in with

.url = "file:///home/.../zsp-user-01/9ced743d434527147a9a61cf11c3db33201f24a3.tar.gz",

fixed the hash, cleared Zig cache both in the project folder and my home folder, …and I’m still getting this error:

$ zig build
error: invalid option: -Dopenssl
/home/archie/.night.zig/zig-linux-x86_64-0.12.0-dev.3123+147beec7d/lib/std/Build.zig:1873:35: 0x1143eb9 in dependency__anon_16347 (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/zsp-user-01/build.zig:43:29: 0x10fab0c in build (build)
    const zsp = b.dependency("zigStructPrint",
                            ^

I could swear I’ve seen different errors before.


So, long story short, could somebody please point me to a simple pair of Zig projects, a library that is accessible via Zig package manager, and a project using that library?

I don’t get it, your zigStructPrint/build.zig at main · Durobot/zigStructPrint · GitHub doesn’t define a build option openssl. Why do you try to use a build option that doesn’t exist?

Here is an example where I define a build option: customerrors-sketch/build.zig at c615170cbae5df06fe92c22e900fb0a6b93fd900 · SimonLSchlee/customerrors-sketch · GitHub


I moved this to a new topic, because the original posters question was answered and we are now exploring a new case and the focus has shifted to your case.