Build.zig for static library in C/C++

Yep, documentation is still a little light, but I think I got you! To follow along, run zig init in a blank directory, and then add the following lines to your build.zig:


// Creating an all C/C++ "em-lib" Static Library
const em_lib = b.addStaticLibrary(.{
    .name = "emlib",
    .target = target,
    .optimize = optimize,
});
em_lib.addCSourceFiles(.{ .files = &.{"em-lib/emlib.c"} });
em_lib.addIncludePath(b.path("em-lib"));
em_lib.linkLibC();
em_lib.linkLibCpp();
em_lib.installHeader(b.path("em-lib/emlib.h"), "em-lib/emlib.h");
b.installArtifact(em_lib);

// Linking in "emlib" to our main executable + including the header path
exe.linkLibrary(em_lib);

I created a folder em-lib which has:

emlib.c:

#include "emlib.h"

int doSomething(int a)
{
    return a + 5;
}

emlib.h:

#pragma once

int doSomething(int a);

And modified main.zig to look like:

const std = @import("std");
const emlib = @cImport({
    @cInclude("em-lib/emlib.h");
});

pub fn main() !void {
    // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
    std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
    
    // Testing "emlib"!
    std.debug.print("Calling a C function: {d}\n", .{emlib.doSomething(10)});

    // stdout is for the actual output of your application, for example if you
    // are implementing gzip, then only the compressed bytes should be sent to
    // stdout, not any debugging messages.
    const stdout_file = std.io.getStdOut().writer();
    var bw = std.io.bufferedWriter(stdout_file);
    const stdout = bw.writer();

    try stdout.print("Run `zig build test` to run the tests.\n", .{});

    try bw.flush(); // don't forget to flush!
}

test "simple test" {
    var list = std.ArrayList(i32).init(std.testing.allocator);
    defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
    try list.append(42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}

When I run this I now get:

All your codebase are belong to us.
Calling a C function: 15
Run zig build test to run the tests.

Does this hopefully give you enough to go on/get started? Also check out this post if you haven’t yet:

1 Like