#Illegal instruction error when using C library

1 messages · Page 1 of 1 (latest)

crimson zephyr
#

I get an error at runtime when trying to write some simple code importing the Allegro 5 C library, here's that code:

const std = @import("std");
const al = @cImport({
    @cInclude("allegro5/allegro5.h");
});

pub fn main() void {
    _ = al.al_init();

    const display = al.al_create_display(320, 200);
    defer al.al_destroy_display(display);
}

(do we really not have markdown code support for zig? sadge)

Specifically, the code gets called when creating the display. Here's the full error message:

Illegal instruction at address 0x7ff819793e7b
???:?:?: 0x7ff819793e7b in ??? (libdispatch.dylib)
Unwind information for `libdispatch.dylib:0x7ff819793e7b` was not available, trace may be incomplete

???:?:?: 0x65707974687464 in ??? (???)
run allegro-test: error: the following command terminated unexpectedly:
/Users/mak/dev/zig/allegro-test/zig-out/bin/allegro-test 
Build Summary: 3/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run allegro-test failure
error: the following build command failed with exit code 1:
/Users/mak/dev/zig/allegro-test/zig-cache/o/6f137061620a950d5546c148bfc6d367/build /Users/mak/dev/_SDKs/zig/zig /Users/mak/dev/zig/allegro-test /Users/mak/dev/zig/allegro-test/zig-cache /Users/mak/.cache/zig run

I read that Zig enables UBSan by default, so I tried adding exe.disable_sanitize_c = true to see if that works, but it doesn't. I also tried using the ReleaseFast optimize option, but that didn't work either, instead producing a new error that I can't post due to character limit.

dusk dock
#

ive noticed that exe.disable_sanitize_c = true only works in some cases, try adding "-fno-sanitize=undefined" to your cflags in build.zig

crimson zephyr
#

how do i add that?

#

or where* i guess

dusk dock
#

are you using exe.addCSourceFile(s)?

crimson zephyr
#

no, here's my build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = std.zig.CrossTarget{ .cpu_arch = .x86_64 };
    const optimize = std.builtin.OptimizeMode.ReleaseFast;

    const exe = b.addExecutable(.{
        .name = "allegro-test",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.addIncludePath(.{ .path = "/usr/local/Cellar/allegro/5.2.8.0_2/include" });
    exe.addLibraryPath(.{ .path = "/usr/local/Cellar/allegro/5.2.8.0_2/lib" });
    exe.linkLibC();
    exe.linkSystemLibrary("allegro");
    exe.linkSystemLibrary("allegro_font");
    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
dusk dock
#

oh

#

huh

#

if its a system lib im not sure what you can do lol

crimson zephyr
#

its not built into macos if that's what you mean, it was downloaded from homebrew

dusk dock
#

i just mean the fact that its already compiled

crimson zephyr
#

ah

dusk dock
#

although
now that i think about it that should make it work fine unless it was already compiled with ubsan

#

which would be odd

crimson zephyr
#

i heard ReleaseFast disables ubsan, which is why i tried that

#

oh, lemme post that error i couldnt post in the OP:

run allegro-test: error: the following command terminated unexpectedly:
/Users/mak/dev/zig/allegro-test/zig-out/bin/allegro-test 
Build Summary: 3/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run allegro-test failure
error: the following build command failed with exit code 1:
/Users/mak/dev/zig/allegro-test/zig-cache/o/5c880ca27111672184d281b5a77850dd/build /Users/mak/dev/_SDKs/zig/zig /Users/mak/dev/zig/allegro-test /Users/mak/dev/zig/allegro-test/zig-cache /Users/mak/.cache/zig run
#

this was with ReleaseFast