I’m getting a segmentation fault when trying to call an exported function from a dynamically loaded Zig library. The same code works when I use std.fs.File.stdout().writeAll() instead of std.debug.print(). It also works fine if i link it directly with zig build-exe main.zig -L./ -ltest -lc.
Segmentation fault at address 0x0
/nix/store/*hash*-zig-0.15.2/lib/zig/std/Progress.zig:660:34: 0x7ff0927030c8 in unlockStderrWriter (std.zig)
pub fn unlockStderrWriter() void {
^
/nix/store/*hash*-zig-0.15.2/lib/zig/std/debug.zig:219:36: 0x7ff0926d6f9c in unlockStderrWriter (std.zig)
std.Progress.unlockStderrWriter();
^
/nix/store/*hash*-zig-0.15.2/lib/zig/std/debug.zig:230:29: 0x7ff0927a5db0 in print__anon_22560 (std.zig)
defer unlockStderrWriter();
^
/home/jerpo/prog/zig/aoc2025/test.zig:4:20: 0x7ff0927a5cb1 in test_fn (test.zig)
std.debug.print("Hello, world!", .{});
^
/home/jerpo/prog/zig/aoc2025/main.zig:9:12: 0x1139d6f in main (main.zig)
test_fn();
^
/nix/store/*hash*-zig-0.15.2/lib/zig/std/start.zig:627:37: 0x113a241 in main (std.zig)
const result = root.main() catch |err| {
^
???:?:?: 0x7ff09282a4d7 in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7ff09282a4d7` was not available, trace may be incomplete
???:?:?: 0x7ff09282a59a in ??? (libc.so.6)
/nix/store/*hash*-zig-0.15.2/lib/zig/libc/glibc/sysdeps/x86_64/start.S:115:0: 0x115fe50 in ??? (/nix/store/*hash*-zig-0.15.2/lib/zig/libc/glibc/sysdeps/x86_64/start.S)
call *__libc_start_main@GOTPCREL(%rip)
aborted (core dumped)
Whats the ldd / readelf output for your binary and shared lib? I feel like this is NativePaths.zig in the compiler messing up things again. Zig has some nixos specific code that i personally dont agree with.
That does look correct to me, do you get same issue if you target -Dtarget=x86_64-linux-gnu explicitly. You could try x86_64-linux-musl as well, but you need to have shell with musl or point to it with LD_LIBRARY_PATH
Your library is completely loaded with correct setup for data, code, etc.
Whatever happens seems unrelated to the loader and related to the code in lib/std/Progress.zig
Since there is no writev call for Hello World, it must crash during the zig flush call for stderr. Using a debugger might help you find what is wrong.
I’m slightly concerned the amount of nix specific code in NativePaths.zig creeping up. The proper way in nix to inject dependencies is to use pkg-config. The interpretation of nixpkgs specific flags in NativePaths.zig is often more harmful than good.
I’m questioning the purpose of the nixpkgs specific flag handling and why is it necessary?
If the main purpose is to have rpaths added to binary automatically so that it “just works” in dev envs. I think this should be done by linkSystemLibrary instead when zig isn’t given a custom --prefix. When a --prefix is given the binaries should be clean as possible. This alone would allow to remove all the nix specific code from NativePaths.zig for this use case.
I’d also deprecate the current linkSystemLibrary behaviour where both search paths and pkg-config are tried. I’d default to using pkg-config on linux (and maybe bsd, they have pkg-config drop-in alternative called pkgconf). Search paths only work in well known environment, and the current auto behavior can give you false impression of your build being portable when it actually isn’t.
example: https://github.com/raysan5/raylib/pull/4123https://github.com/raysan5/raylib/pull/4406
On macos, pkg-config is still useful if you want to integrate with homebrew or nixpkgs for example, but other than that system libraries in macos should be read from --sysroot, so search path is good default there I think. On macos + nix, nix actually ships their own macos sdk, and their own xcbuild system. I was about to note about the absolute paths that prevent nix’s xcbuild system to be used by zig, but it seems it was already fixed https://github.com/ziglang/zig/commit/92b20e42162675d35ffd27845e66b0f9213c00c2
When everything else fails, the final escape hatch for environments that don’t play nice with pkg-config is often LDLIBS, LDFLAGS, CFLAGS and friends…