Skip to content

42LM/zig-package-manager-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zig-package-manager-example

This is a small and simple example/demonstration of the zig package manager aka zig.zon.

It provides an example on how to provide a zig ligrary and how to use it in a different project.

Important

🚨 Does not work on zig versions below < v0.14.0!

The example works on the current zig version 0.16.0-dev.1859+212968c57.

If you search for examples for older zig versions check out the older releases/tags. The releases/tags in this repo are sorted by zig version.

Tip

If you are looking for minimal setup of a zig library please check out the minimal branch.

Using it

Create a new zig project:

mkdir zig-package-manager-example
cd zig-package-manager-example
zig init

In your zig project folder (where build.zig is located), run:

zig fetch --save "git+https://github.com/42LM/zig-package-manager-example"

Tip

You can also fetch the lib/repo via tag/release. Check out the v0.0.0 release.

Then, in your build.zig's build function, add the following before b.installArtifact(exe):

    const hello = b.dependency("zig_package_manager_example", .{
        .target = target,
        .optimize = optimize,
    });

Add the module as import:

    const exe = b.addExecutable(.{
        .name = "foo",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{
                .{ .name = "foo", .module = mod },
                .{ .name = "hello", .module = hello.module("hello") }, // <<<
            },
        }),
    });

In your projects main.zig file import the hello module:

const std = @import("std");
const hello = @import("hello");

pub fn main() !void {
    std.debug.print("{s}\n", .{hello.world()});
}

Note

Add the module as import to your lib:

    const root = b.addModule("root", .{
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "hello", .module = hello.module("hello") }, // <<<
        },
    });

    const lib = b.addLibrary(.{
        .linkage = .static,
        .name = "root",
        .root_module = root,
    });

Run the project:

zig build run

Does not do much, only prints out no operations

Run the tests:

zig build test

Run the tests of the lib

Troubleshoot

Warning

Handle with care: Delete the cache of zig:

rm -rf ~/.cache/zig