0Pricing
Zig Academy · Lesson

Adding a Third-Party Module

Fetch and link an external package.

Adding a Third-Party Module is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Manifest to Code

A dependency listed in build.zig.zon is downloaded, but not yet usable. You must wire it into build.zig to reach it in code. 🔌

Step One: zig fetch

Start by adding the package. zig fetch with --save downloads it and records its url and hash in your manifest automatically.

zig fetch --save https://example.com/mylib-1.0.0.tar.gz

Look It Up in build.zig

In build.zig, call b.dependency with the name from your manifest. It returns a handle to that fetched package.

const dep = b.dependency("mylib", .{});

Get the Module

From that handle, ask for a named module the package exposes. The author documents which module name to request.

const mylib = dep.module("mylib");

Attach It to Your Build

Make the module importable by adding it to your executable with addImport, choosing the name you will use in code.

exe.root_module.addImport("mylib", mylib);

Now @import It

Once attached, the name behaves like std. Use @import with that string anywhere in your source to bring the library in.

const mylib = @import("mylib");

Call the Library

With the import bound to a const, reach into its pub functions and types exactly like any other module you wrote.

const result = mylib.doThing(42);

Pass Build Options

The second argument to b.dependency forwards options like target and optimize, so the dependency builds for the same settings as you.

const dep = b.dependency("mylib", .{
    .target = target,
});

Link a C Library Too

Some packages ship a static library, not just a module. You can linkLibrary against its artifact when the package provides one.

exe.linkLibrary(dep.artifact("mylib"));

Build and Run

Finally run zig build. Zig fetches anything missing, compiles the dependency, and links it into your program in one command.

zig build run

Update by Re-fetching

To move to a newer version, run zig fetch again with the new url. It refreshes the hash so the next build pulls the updated code.

zig fetch --save https://example.com/mylib-1.1.0.tar.gz

Quick Check

Recall the build.zig call that makes a dependency's module importable.

Recap

You wired a dependency end to end: zig fetch, then b.dependency, module, and addImport in build.zig, and finally @import in your code. 🎯

Frequently asked questions

Is the “Adding a Third-Party Module” lesson free?

Yes — the full text of “Adding a Third-Party Module” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Adding a Third-Party Module”?

Fetch and link an external package. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Adding a Third-Party Module” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Importing Files with @import
  2. Structuring a Multi-File Project
  3. build.zig.zon and the Package Manager
  4. Adding a Third-Party Module
← Back to Zig Academy