Search code examples
segmentation-faultx11zig

I get Segmentation fault when using X11 C library with Zig


I'm trying to use X11 with Zig. Using the code from https://rosettacode.org/wiki/Window_creation/X11#Xlib as an example, I came up with this minimal example:

const c = @cImport({
    @cInclude("X11/Xlib.h");
});

pub fn main() void {
    var maybe_display : ?*c.Display = c.XOpenDisplay(@as(?*u8, null));
}

Compiling with the newest version of Zig (0.6.0):

zig build-exe -lX11 main.zig

At execution I get the following error:

Segmentation fault at address 0x0
attempt to unwrap error: InvalidExe
Panicked during a panic. Aborting.
fish: './main' terminated by signal SIGABRT (Abort)

Solution

  • You probably also need to link against libc. Try adding -lc.