How to build x11/wayland programs when linking with musl

I want to use zig cc/c++ with --target=x86_64-linux-musl to build for linux.
What’s a good way to link against x11 and/or wayland ?

Using pkgconfig:

zig cc $(pkg-config --cflags --libs x11) ...

The result on my system (Debian) is zig cc -lX11 ... and requires apt install libx11-dev.


pkgconfig is also used by build.zig when calling linkSystemLibrary:

    exe.linkSystemLibrary("x11");

That doesn’t seem to work when targeting musl
error: unable to find dynamic system library 'X11' using strategy 'paths_first'. searched paths: none

try adding --static in $(pkg-config --cflags --libs --static x11)

zig cc --target=x86_64-linux-musl  -lX11 -lpthread -lxcb -lXau -lXdmcp -o main main.c
error: unable to find dynamic system library 'X11' using strategy 'paths_first'. searched paths: none
error: unable to find dynamic system library 'xcb' using strategy 'paths_first'. searched paths: none
error: unable to find dynamic system library 'Xau' using strategy 'paths_first'. searched paths: none
error: unable to find dynamic system library 'Xdmcp' using strategy 'paths_first'. searched paths: none

What is your goal with using -target x86_64-linux-musl?

If you are on a musl based system, then just having the correct X11/Wayland packages installed and using zig cc with the given -l flags (or via pkgconfig) should work to make a dynamic executable.

If you want to produce a static executable (the default zig cc behavior for -target x86_64-linux-musl) then you are going to have to build static musl linked versions of e.g. libwayland.a and its dependencies. Unfortunately you won’t be able to use OpenGL or Vulkan from a static executable though.

Or are you trying to cross compile a dynamic executable to target musl based Linux systems (Alpine, Chimera, Void, etc.)?

I’m trying to target musl-based linux systems as well as linux distros with a very old version of glibc. I want it to cover as many systems as possible, basically.