Simple example loading a dynamic library with zig. The dynamic library can be built using: zig build. The main script will then load the library and call the functions by running zig run main.zig.
Output on Ubuntu 22.04.5 LTS using Zig compiler 0.14.0 where the number returned by give keeps changing:
Dynamic lib path: zig-out/lib/libzigdyn.so
Dynamic lib give: 1830420592
Dynamic lib add: 1+2=1830420593
Dynamic lib sub: 1-2=1830420591Expected output:
Dynamic lib path: zig-out/lib/libzigdyn.so
Dynamic lib give: 1
Dynamic lib add: 1+2=3
Dynamic lib sub: 1-2=-1I tried the zig 0.11 compiler and it gave the expected output so I expect the problem is in my main.zig.
Update: Fixed by adding callconv(.C) to the function pointers.
g++ -o main main.cpp -ldl
./mainOutput (as expected):
3
-1
1