Skip to content

42LM/zig-package-manager-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ubuntu-latest macos-latest

v0.12.0 v0.13.0 v0.14.0

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.12.0!
⚠️ Does not work on windows!

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("hello", .{
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addImport("hello", 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()});
}

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