Zig 0.14.0 fails at building this simple .so and dynamically invoking it.
lib.zig:
pub export fn SetInt(ptr : *i32) void {
ptr.* = 345;
}
main.zig:
const std = @import("std");
const SetIntFn = *const fn (ptr : *i32) void;
pub fn main() !void {
var lib = try std.DynLib.open("zig-out/lib/libmathlib.so");
defer lib.close();
const set_int = lib.lookup(SetIntFn, "SetInt") orelse std.debug.panic("ARG!", .{});
var i : i32 = 1;
set_int(&i);
std.debug.print("i: {}\n", .{i});
}
build.zig:
const std = @import("std");
pub fn build(b: *std.Build) void {
// Build the DLL
const lib = b.addSharedLibrary(.{
.name = "mathlib",
.root_source_file = b.path("src/lib.zig"),
.target = b.graph.host
});
// Build the executable
const exe = b.addExecutable(.{
.name = "main",
.root_source_file = b.path("src/main.zig"),
.target = b.graph.host
});
// Install both the DLL and the executable
b.installArtifact(lib);
b.installArtifact(exe);
}
It builds just fine with no errors or warnings. It runs just fine except it outputs:
i: 1
Why is this not working? What am I missing, for the record, I tried adding link_libc to switch it between ElfDynLib and DlDynLib, both dynamic implementations fails on pure zig dynamic libraries.