🎓 A1Learner
🔍
0 of 46 lessons completed

Zig Build Artifacts

A build artifact is a file a build produces, such as a program or a library. The install step copies each artifact into its folder under zig-out.

Install a program artifact

This build file installs the program as its only artifact. The plain build command copies it to zig-out/bin.

const std = @import("std");
pub fn build(b: *std.Build) void {
    const exe = b.addExecutable(.{ .name = "app", .root_source_file = b.path("src/main.zig"), .target = b.standardTargetOptions(.{}), .optimize = b.standardOptimizeOption(.{}) });
    b.installArtifact(exe);
}

Install a library artifact

This build file installs a static library as its only artifact. The plain build command copies it to zig-out/lib.

const std = @import("std");
pub fn build(b: *std.Build) void {
    const lib = b.addStaticLibrary(.{ .name = "calc", .root_source_file = b.path("src/calc.zig"), .target = b.standardTargetOptions(.{}), .optimize = b.standardOptimizeOption(.{}) });
    b.installArtifact(lib);
}

Use a library in a program

The program imports the library code as a module. Its run step prints the sum from the library function.

const std = @import("std");
pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const lib = b.addStaticLibrary(.{ .name = "calc", .root_source_file = b.path("src/calc.zig"), .target = target, .optimize = optimize });
    b.installArtifact(lib);
    const exe = b.addExecutable(.{ .name = "app", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize });
    exe.root_module.addImport("calc", b.createModule(.{ .root_source_file = b.path("src/calc.zig") }));
    b.installArtifact(exe);
    const run_step = b.step("run", "Run the app");
    const run = b.addRunArtifact(exe);
    run_step.dependOn(&run.step);
}

Example

const std = @import("std");
pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const lib = b.addStaticLibrary(.{ .name = "calc", .root_source_file = b.path("src/calc.zig"), .target = target, .optimize = optimize });
    b.installArtifact(lib);
    const exe = b.addExecutable(.{ .name = "app", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize });
    exe.root_module.addImport("calc", b.createModule(.{ .root_source_file = b.path("src/calc.zig") }));
    b.installArtifact(exe);
    const run_step = b.step("run", "Run the app");
    const run = b.addRunArtifact(exe);
    run_step.dependOn(&run.step);
}

Key points

  • Build artifact: a build artifact is a file a build produces.
  • Install step: an install step is the build work that copies artifacts into their install folders.
  • Program artifact: a program artifact is a finished program file under bin.
  • Library artifact: a library artifact is a compiled library file under lib.
  • Static library: a static library is a file of compiled code that other programs link into themselves.
  • Install folder: an install folder is the folder under zig-out that holds one kind of artifact.

The build file makes two artifacts, the library and the program. The program imports the library module and prints the doubled value. The install lines also place the program under bin and the library under lib.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Where does the install step copy a program artifact?