Zig can't find header files for system library

Hello, i was trying to make a simple program to learn the pipewire api but i cannot get zig to find the header files

i’m using a simple build file and adding the library with linkSystemLibrary() but i don’t know if there is something else i have to do to add the include directories, or if this is just a bug because all examples i could find only use linkSystemLibrary()

i’m on NixOS so i don’t want to have to hardcode the paths to the library

heres the build.zig

const std = @import("std");

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

    const exe = b.addExecutable(.{
        .name = "hello",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    exe.linkSystemLibrary("pipewire-0.3");
    exe.linkLibC();

    b.installArtifact(exe);

    const run_exe = b.addRunArtifact(exe);

    const run_step = b.step("run", "Run the program");
    run_step.dependOn(&run_exe.step);
}

and here is main.zig

const std = @import("std");
const pw = @cImport({
    @cInclude("pipewire/pipewire.h");
});

const print = std.debug.print;

pub fn main() void {
    pw.pw_init(null, null);

    print("header version {s}/n", .{pw.pw_get_headers_version()});
}

btw i already tried compiling the pipewire example in c and it does work so it is not my nix config

If your system includes are in a non-standard location, you might want the --search-prefix option. You might also want to take a peek at addSystemIncludePath.

Users of zig build may use --search-prefix to provide additional directories that are considered “system directories” for the purposes of finding static and dynamic libraries.

Source: Zig Build System ⚡ Zig Programming Language

3 Likes

thats not really what i was looking for but it may be the only way

supposedly the build system uses pkg-config to find the libraries so i thought it should also be able to use that to find the include directories since pkg-config includes them with --cflags but if it doesn’t do that and there is no way for it to do that from within build.zig then that kinda sucks

when you run pkg-config pipewire-0.3 --cflags do you get any -I directories?

yes it gives me this

-D_REENTRANT
-I/nix/store/n4865ip986ymcsiwblm5ff89ybln9nw1-pipewire-1.2.5-dev/include/pipewire-0.3
-I/nix/store/n4865ip986ymcsiwblm5ff89ybln9nw1-pipewire-1.2.5-dev/include/spa-0.2

try:

    exe.linkSystemLibrary2("pipewire-0.3", .{
        .use_pkg_config = .force,
    });
3 Likes

that worked!

i had to add lib to the name but it worked

    exe.linkSystemLibrary2("libpipewire-0.3", .{
        .use_pkg_config = .force,
    });
3 Likes