Dynamic Library Loading Error

Hello everyone,

I am a fresh user in the forum, even if I have been using Zig for some years now. I am looking for some pointers to file a bug report (if it’s actually a bug). The problem comes from this simple program:

const std = @import("std");

pub fn main() !void {
var vk_lib = try std.DynLib.open("libvulkan.so.1");

const Loader = *fn (?*anyopaque, [*:0]const u8) callconv(.c) ?*anyopaque;
const loader = vk_lib.lookup(Loader, "vkGetInstanceProcAddr")
    orelse return error.SymbolNotFound;

std.debug.print("loader: {}\n", .{ loader });

const res = loader(null, "vkCreateInstance");
std.debug.print("res: {*}", .{ res });

}

const spv align(@alignOf(u32)) = @embedFile("shader").*;
const vk = @import("vulkan");

When I run it compiling without linking to libc I get a runtime error, while if I link libc everything works fine. I would say there is a problem from the elf loading std.

What should I do?

If you do not want to link libc, but you still want a portable static executable and the ability to load dynamic libraries, you can try https://github.com/TibboddiT/dyn-loader.

There is a simple example that loads Vulkan in the repo. There is also a more involved example that draws a triangle using Vulkan.

If you try it and it does not work, please let me know :slight_smile:

2 Likes