#Call a .so function; Error Not an ELF file

1 messages · Page 1 of 1 (latest)

celest karma
#

I am trying to call a .so file from zig; (on Linux; I would also like it to be able to work on Windows)
This is the file I am compiling to a .so (zig build-lib math.zig) (I then rename the output to liba.so)

Other info :
Arch Linux
Zig 0.15.1

math.zig:

export fn add(x : f32, y : f32) callconv(.c) f32 {
    return x + y;
}

main.zig :

const std = @import("std");
const print = std.debug.print;

pub fn main() !void {
    var dynLib = try std.DynLib.open("./liba.so");
    defer dynLib.close();
    const math_fn = dynLib.lookup(*const fn (f32, f32) callconv(.c) f32, "add").?;

    const r = math_fn(10.0, 5.0);
    print("Res : {d:.3}\n", .{r});
}

Error :

error: NotElfFile
*(my_path)*/lib/std/dynamic_library.zig:245:56: 0x113fcb9 in open (std.zig)
        if (!mem.eql(u8, eh.e_indent[0..4], elf.MAGIC)) return error.NotElfFile;
*(my_path)*/lib/std/dynamic_library.zig:32:28: 0x113e016 in open (std.zig)
        return .{ .inner = try InnerType.open(path) };
*(my_path)*/dyn/ex.zig:5:18: 0x113c780 in main (ex.zig)
    var dynLib = try std.DynLib.open("./liba.so");
celest karma
outer vapor
#

it creates a static library otherwise

celest karma
#

What's the difference?

#

Is it just that dynamic can be called at runtime

#

But static only effectively at comptime?