# Importing a library with examples

**URL:** <https://ziggit.dev/t/importing-a-library-with-examples/4384>\
**Category:** Help\
**Tags:** build-system\
**Created:** [May 20, 2024, 10:12am UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384 "2024-05-20T10:12:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 10:12am UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/1 "2024-05-20T10:12:49Z")

</div>

As a newbie to programming, I made a toy project with this guide: [Build system tricks](https://ziggit.dev/t/build-system-tricks/3531)

It’s going well, but still can’t find a solution so that when importing, errors don’t appear regarding the missing examples.

All the code is here: [ztester](https://github.com/7R35C0/ztester.git)

- ztester - is the library with examples (minus zimport)
- zimport - is the project which import ztester

Tested only with zig version 0.12.0 on Linux Fedora 39.

Build and run the project result in:

```txt
zig build run
ztester: error.FileNotFound
Hello zimport

```

The error `ztester: error.FileNotFound` means that the `examples` directory from `ztester` library is missing in `zimport`.

This fact seems somewhat strange to me because no step from the build library is propagated to the top-level install step. From documentation, `addInstallArtifact` merely creates the step; it does not add it to the dependencies of the top-level install step.

The only solution have found so far is to make a separate build, without examples, for the `ztester.tar.gz` package.

Is there a better solution?

And one last question, creating a module only makes sense from the perspective of an already compiled library that is linked by user, such as a shared or static library, correct?

If the library is a dependency, @import(“ztester”) works as expected. No need for a module (or a build.zig), at least that’s what I noticed.

Sorry for my childish problems, but couldn’t find a solution on my own. Like I said, I have no programming experience.

Thanks in advance for your help.

All the best!!!

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [May 20, 2024, 10:22am UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/2 "2024-05-20T10:22:01Z")

</div>

Hello @7R35C0 Welcome to ziggit 🙂

In ztester `build.zig.zon` `.paths` you must add `"examples",`  
see: [build.zig.zon#paths](https://github.com/ziglang/zig/blob/master/doc/build.zig.zon.md#paths)

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 10:39am UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/3 "2024-05-20T10:39:59Z")

</div>

Sorry but I do not understand.

Maybe I made myself difficult to understand by putting the `ztester` library together with `zimport`, which is an independent project, I had to make two repositories.

However, what does `.path` from `ztester` `build.zig.zon` have to do with `examples`, they are not imported, they are not actually part of the final library.

Thanks for your help! 🙏

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 11:00am UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/4 "2024-05-20T11:00:56Z")

</div>

Adding `examples` to `.paths` in `ztester` `build.zig.zon` does not remove the error.  
I realized that maybe you don’t mean `dependency.path`

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 11:14am UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/5 "2024-05-20T11:14:06Z")

</div>

Another try with `examples` in `.paths` and in package content, not working.

---

<div class="post-metadata">

**Author:** ![Calder-Ty](https://ziggit.dev/user_avatar/ziggit.dev/calder-ty/32/10237_2.png) [@Calder-Ty](https://ziggit.dev/u/Calder-Ty)\
**Post date:** [May 20, 2024, 1:14pm UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/6 "2024-05-20T13:14:56Z")

</div>

Could you share the build.zig.zon? It’s hard to help without concrete details

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 1:18pm UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/7 "2024-05-20T13:18:58Z")

</div>

All the code is here: [ztester](https://github.com/7R35C0/ztester.git)

- ztester - is the library with examples (minus zimport)
- zimport - is the project which import ztester

The dependency is from latest release.

I tried with and without `examples` in `ztester` `build.zig.zon` `.paths` and package, still the error occurs.

Thanks! 🙏

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [May 20, 2024, 1:28pm UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/8 "2024-05-20T13:28:45Z")

</div>

I am sorry, wrong fix.

The problem is in:

```zig
fn setupExamples(b: *std.Build, cfg: Config, mod: *std.Build.Module) void {
    var egs_dir = std.fs.cwd().openDir(
        "examples",

```

It tries to find examples in current working directory, which is fine when build is called from ztester but it is not fine when build is called from zimport.

The following code, opens the “examples” in the `ztester` “build.zig” directory:

```zig
fn setupExamples(b: *std.Build, cfg: Config, mod: *std.Build.Module) void {
    var egs_dir = std.fs.openDirAbsolute(
        b.path("examples").getPath(b),
        .{ .iterate = true },
    ) catch |err| {
        print("{s}: {!}\n", .{ cfg.name, err });
        return;
    };
    defer egs_dir.close();

```

To use the latest `ztester` code, change the `zimport/build.zig.zon` to:

```zig
.{
    .name = "zimport",
    .version = "0.0.0",
    //.minimum_zig_version = "0.12.0",

    .dependencies = .{
        .ztester = .{ .path = ".." },
    },

    .paths = .{
        "src",
        "build.zig",
        "build.zig.zon",
        "LICENSE",
        "README.md",
    },
}

```

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 1:54pm UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/9 "2024-05-20T13:54:58Z")

</div>

No problem, I wasn’t clear enough either.

Your solution solves the problem when a local path is used.  
With a remote package (`.url` and `.hash`), the error persists.

Do I need to add `examples` folder to `ztester` `build.zig.zon` `.paths` and package?  
I honestly don’t see why it would be necessary.

Thanks!

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 2:14pm UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/10 "2024-05-20T14:14:44Z")

</div>

Yes, finally it seems to be working.

I had to add `examples` in `zon` and package.

```zig
.paths = .{
        "examples",
        "src",
        "build.zig",
        "build.zig.zon",
        "LICENSE",
        "README.md",
    },

```

I don’t have enough words to thank you for your help.  
All last week I struggled to solve the problem. 😅

Thank you very much! 🙏 🙏 🙏

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 2:30pm UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/11 "2024-05-20T14:30:47Z")

</div>

All the code is up to date here, [ztester](https://github.com/7R35C0/ztester.git) if anyone needs it.

Thank you all!

---

<div class="post-metadata">

**Author:** ![7R35C0](https://ziggit.dev/user_avatar/ziggit.dev/7r35c0/32/2393_2.png) [@7R35C0](https://ziggit.dev/u/7R35C0)\
**Post date:** [May 20, 2024, 5:28pm UTC](https://ziggit.dev/t/importing-a-library-with-examples/4384/12 "2024-05-20T17:28:51Z")

</div>

For this small [zdirwalker](https://github.com/7R35C0/zdirwalker) library, I needed to stop the examples error when importing into another project.

Thanks!
