Depend on boost with zig build-system

Im trying to built my C/C++ project with zig. In the project I am using the boost libraries and have tried to add them to my build.zig (see code below) but I get a panic: unable to find artifact ‘boost’

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const boost_dep = b.dependency("boost", .{
        .target = target,
        .optimize = optimize,
    });

    const exe = b.addExecutable(.{
        .name = "main",
        .optimize = optimize,
        .target = target,
    });

    exe.linkLibrary(boost_dep.artifact("boost"));

    exe.linkLibC();
    exe.linkLibCpp();
    exe.addIncludePath(.{ .path = "src" });
    exe.addCSourceFiles(.{ .files = &source_files });

    b.installArtifact(exe);
}

const source_files = [_][]const u8{
    "src/shell.cpp",
};

const flags = [_][]const u8{
    "-std=c++20",
};

In my built.zig.zon file I have:

.boost = .{
            .url = "https://github.com/boostorg/boost/archive/refs/tags/boost-1.83.0.tar.gz",
            .hash = "1220f4e2c01c2c79272b7fd4acd29ba24c63cb4221b938a0d6cf89a60e33f3ef8981",
        },

The approach doesn’t have to be like mine I just want to be able to user my header files.
Thanks for your help.

Hello @WehrWolff Welcome to ziggit :slight_smile:

Packages can expose modules and artifacts with a build.zig file.
Since boost does not expose any; only access to the archived files is available.
You can access the files using boost_dep.path

I would assume that I have to do something like exe.installHeader where I pass in the boost_dep.path however that takes an argument sub_path. I don’t know what that value should be in my case or what zig expects/does with it/needs it for.

pub fn path(d: *Dependency, sub_path: []const u8) LazyPath

Thanks again for the help :grinning:

With sub_path you specify the file within the dependency that you want to access.

Here is another topic about using boost with zig:

So the way I understood it is if I in my c++ files have something like #include <boost/serialization/map.hpp> then I should pass in "/serialization/map.hpp" as sub_path.

However that wasn’t working for me (I get file not found error). Is the url (with the hash) enough or do I need to have it included in the project in a deps folder (or something similar)? And if so is there an other way of doing it because I’d rather not end up with a deps folder with the size of node_modules.

The url and hash is enough.
If you download the url and look inside the the .tar.gz archive then whatever you pass to sub_path should exist within there, I think the path should be specified without a leading /.

2 Likes