ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.78k stars 2.54k forks source link

Code using GLX fails, but only on FreeBSD #14873

Open stefncb opened 1 year ago

stefncb commented 1 year ago

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

Here's the Zig code for a minimal program that requests all valid framebuffers from GLX:

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

pub fn main() !void {
    const display = c.XOpenDisplay(null) orelse @panic("display");

    const visual_attribs = [_]c_int{
        c.None,
    };

    var fbc_count: c_int = undefined;
    _ = c.glXChooseFBConfig(
        display,
        c.DefaultScreen(display),
        &visual_attribs,
        &fbc_count,
    ) orelse @panic("fbc");
}

To build this, here's what I had to add to the default build.zig (the include and lib paths are because of FreeBSD):

exe.addLibraryPath("/usr/local/lib");
exe.addIncludePath("/usr/local/include");
exe.linkSystemLibrary("X11");
exe.linkSystemLibrary("xcb");
exe.linkSystemLibrary("Xau");
exe.linkSystemLibrary("Xdmcp");
exe.linkSystemLibrary("GL");

If I build this on a FreeBSD machine (both with a just-compiled compiler from master and with the 0.10.0 one), it panics at the penultimate line. On Linux it runs fine.

Here's the same program in C:

#include <X11/Xlib.h>
#include <GL/glx.h>
#include <assert.h>

int main(void) {
    Display* display = XOpenDisplay(0);
    assert(display != 0);

    int visual_attribs[] = {
            None,
    };

    int fbc_count;
    GLXFBConfig* fbc = glXChooseFBConfig(
        display,
        DefaultScreen(display),
        visual_attribs,
        &fbc_count
    );
    assert(fbc != 0);

    return 0;
}

Compile with cc -Wall -Wextra -Werror -L/usr/local/lib -I/usr/local/include -o 'test' 'main.c' -lX11 -lGL. This runs fine on both FreeBSD and Linux.

I've been debugging this for days now. It might be some deep library-was-linked-incorrectly thing that I'm not knowledgeable enough to find, I really can't tell.

Expected Behavior

Both programs should exit normally on both platforms.

lateefj commented 10 months ago

I have not been able to recreate this issue: Zig version:

zig version
0.12.0

FreeBSD Release:

uname -r
14.0-RELEASE