Declaring Executables and Libraries
Add artifacts to the build graph.
Declaring Executables and Libraries is a free Zig Academy lesson on CoddyKit — lesson 2 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.
Artifacts to Build
The things you compile are called artifacts: an executable you run, or a library other code links against. You declare them in build.zig. 📦
Add an Executable
Create a runnable program with addExecutable. You give it a name and the root source file that holds main.
const exe = b.addExecutable(.{
.name = "app",
});Point to the Root Module
The artifact needs a root_module built from your main file, plus the target and optimize values you chose earlier.
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
}),Carry Target and Optimize
That module also takes the target and optimize fields, so the executable honors whatever the command line requested.
.target = target,
.optimize = optimize,Install the Output
Creating an exe is not enough; tell Zig to place it in zig-out with installArtifact so the default build emits it.
b.installArtifact(exe);Where Outputs Land
Installed binaries appear under zig-out/bin by convention. Run zig build and look there for your freshly compiled program.
zig-out/bin/appAdd a Library
For reusable code, use addLibrary instead. It produces a library artifact others can import and link against.
const lib = b.addLibrary(.{
.name = "mathz",
});Static or Dynamic
A library can be static (baked into the caller) or dynamic (a shared file). You set linkage when you declare the library.
.linkage = .static,Link Library into Exe
Connect them so the exe can call the library's code: use linkLibrary to add the dependency edge.
exe.linkLibrary(lib);Need libc?
If your code calls C standard functions, request libc on the artifact with linkLibC so the right system library is linked.
exe.linkLibC();A Graph of Artifacts
Each artifact is a node; linking adds edges. Together they form the build graph Zig walks when you run the build. ✅
Quick Check
Recall which call actually emits a built binary into zig-out.
Recap
You declared artifacts with addExecutable and addLibrary, set their root module and target, then used installArtifact and linkLibrary to wire them. 🎯
Frequently asked questions
Is the “Declaring Executables and Libraries” lesson free?
Yes — the full text of “Declaring Executables and Libraries” 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 “Declaring Executables and Libraries”?
Add artifacts to the build graph. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring Executables and Libraries” 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
- Anatomy of a build.zig File
- Declaring Executables and Libraries
- Build Steps and zig build run
- Build Options and Flags