首页 > 解决方案 > 使用带有 Zig 的 X11 C 库时出现分段错误

问题描述

我正在尝试将 X11 与 Zig 一起使用。以https://rosettacode.org/wiki/Window_creation/X11#Xlib中的代码为例,我想出了这个最小的例子:

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

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

使用最新版本的 Zig (0.6.0) 编译:

zig build-exe -lX11 main.zig

在执行时,我收到以下错误:

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

标签: segmentation-faultx11zig

解决方案


您可能还需要链接到 libc。尝试添加-lc.


推荐阅读