#Zig first timer question: linking with pkg-config

1 messages · Page 1 of 1 (latest)

indigo crypt
#

Hello 👋🏽

I have possibly a silly question, but I can't seem to find a response through google...

First time zig user, long time programmer. I am trying to link a hello world with ffmpeg to validate Zig for a work project, but I seem to be having issues doing so? I can verify I can find the compiled libs using pkg-config, but Zig does not seem to be able to do so.

Here is my 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 = "ffmpeg-demo",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });

    exe.linkLibC();
    const ffmpegLinkOpts = std.Build.Module.LinkSystemLibraryOptions{
        .needed = true,
        .use_pkg_config = .yes
    };

    exe.linkSystemLibrary2("avformat", ffmpegLinkOpts);
    exe.linkSystemLibrary2("avcodec", ffmpegLinkOpts);
    exe.linkSystemLibrary2("avutil", ffmpegLinkOpts);

    b.installArtifact(exe);
}

I get the following error for all three imports:

error: error: unable to find dynamic system library 'avformat' using strategy 'paths_first'. searched paths:
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/libavformat.tbd
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/libavformat.dylib
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/libavformat.so
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk/usr/lib/libavformat.a

Seems like it is in fact not using pkg-config to find the dependencies as I would expect, what am I doing wrong?

Thank you!!

#

if I change the options .use_pkg_config from .yes to .force then it now panics with the following error:

thread 7620698 panic: pkg-config failed for library avformat
~/.asdf/installs/zig/0.13.0/lib/std/Build/Step/Compile.zig:1159:50: 0x100b4cc1f in make (build)
                                            panic("pkg-config failed for library {s}", .{system_lib.name});
                                                 ^
~/.asdf/installs/zig/0.13.0/lib/std/Build/Step.zig:182:13: 0x100b22cbf in make (build)
    s.makeFn(s, prog_node) catch |err| switch (err) {
            ^
~/.asdf/installs/zig/0.13.0/lib/compiler/build_runner.zig:948:31: 0x100ae8013 in workerMakeOneStep (build)
    const make_result = s.make(sub_prog_node);
                              ^
~/.asdf/installs/zig/0.13.0/lib/std/Thread/Pool.zig:102:39: 0x100ae89d3 in runFn (build)
            @call(.auto, func, closure.arguments);
                                      ^
~/.asdf/installs/zig/0.13.0/lib/std/Thread/Pool.zig:191:18: 0x100b5b24b in worker (build)
            runFn(&run_node.data);
                 ^
~/.asdf/installs/zig/0.13.0/lib/std/Thread.zig:408:13: 0x100b215b3 in callFn__anon_13898 (build)
            @call(.auto, f, args);
            ^
~/.asdf/installs/zig/0.13.0/lib/std/Thread.zig:674:30: 0x100ae760f in entryFn (build)
                return callFn(f, args_ptr.*);
                             ^
???:?:?: 0x187c91f93 in ??? (libsystem_pthread.dylib)
???:?:?: 0xe95c000187c8cd33 in ??? (???)
error: the following build command crashed:
~/Documents/code/ffmpeg-demo_zig/.zig-cache/o/05004d4bccdcba20fd0fd47ea2dba39e/build ~/.asdf/installs/zig/0.13.0/zig ~/Documents/code/ffmpeg-demo_zig ~/Documents/code/ffmpeg-demo_zig/.zig-cache ~/.cache/zig --seed 0x824f324c -Z5f0b80a91df733c2 --color on
#

fixed, turns out it needs the full name of lib<name_of_lib> as pkg-config would

reef linden
#

link_libc is not needed if you have exe.linkLibC()

Also I think linkSystemLibrary should be enough without having to specify pkg_config

indigo crypt
#

thank you! it did work as expected, I didnt expect to have to add the lib prefix, but it makes sense

indigo crypt
reef linden
indigo crypt
#

thank you! are these documented anywhere? I am having a really hard tiome finding those myself 🤔

reef linden
indigo crypt
#

i appreciate that! the first link you sent is the one i was using but i couldnt find any of that in there