I tripped into a very mysterious segfault on Linux:
λ ./zig-out/bin/test_sdl
Segmentation fault at address 0x0
???:?:?: 0x0 in ??? (???)
zsh: IOT instruction ./zig-out/bin/test_sdl
from the following code:
const c = @import("c.zig");
pub fn main() !void {
{
const srv = c.sdl.SDL_Init(c.sdl.SDL_INIT_VIDEO);
assert(srv == 0);
}
c.sdl.SDL_Quit();
}
The issue was that I hadn’t linked libc with:
exe.linkSystemLibrary2("c", .{});
Note that I didn’t get any linking errors from Zig whether I had that line or not.
To contrast: compiling a similar program directly in C:
clang main.c -o main -I/usr/include -L/usr/lib/x86_64-linux-gnu/ -lSDL2
gives an executable that works just fine without adding -lc
Is there a bug here or, at the very least, a way to create a better error message than just segfaulting?
I’m not the first to hit this either:
This was kind of annoying to track down as gdb isn’t very helpful:
λ gdb ./zig-out/bin/test_sdl -c /tmp/core-test_sdl.203520.devel-x1.1729560859
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./zig-out/bin/test_sdl...
[New LWP 203520]
Core was generated by `./zig-out/bin/test_sdl'.
Program terminated with signal SIGABRT, Aborted.
#0 0x0000000001038542 in os.linux.x86_64.syscall4 (number=rt_sigprocmask, arg1=2, arg2=140736924885552, arg3=0, arg4=8)
at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/os/linux/x86_64.zig:57
57 return asm volatile ("syscall"
(gdb) bt
#0 0x0000000001038542 in os.linux.x86_64.syscall4 (number=rt_sigprocmask, arg1=2, arg2=140736924885552, arg3=0, arg4=8)
at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/os/linux/x86_64.zig:57
#1 0x0000000001055f2d in os.linux.sigprocmask (flags=2, set=0x7fffde6a2230, oldset=0x0)
at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/os/linux.zig:1634
#2 0x00000000010389d8 in posix.sigprocmask (flags=2, set=0x7fffde6a2230, oldset=0x0)
at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/posix.zig:5703
#3 0x0000000001038953 in posix.raise (sig=6 '\006') at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/posix.zig:729
#4 0x00000000010368df in posix.abort () at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/posix.zig:673
#5 0x000000000103619b in debug.handleSegfaultPosix (sig=11, info=0x7fffde6a25f0, ctx_ptr=0x7fffde6a24c0)
at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/debug.zig:1246
#6 0x00000000010384f0 in ?? () at /home/devel/local/zig-linux-x86_64-0.14.0-dev.1410+13da34955/lib/std/debug.zig:1275
#7 0x0000000000000007 in ?? ()
#8 0x0000000000000000 in ?? ()