Skip to content

ScepticalRabbit/zigdynlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zigdynlib

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=1830420591

Expected output:

Dynamic lib path: zig-out/lib/libzigdyn.so
Dynamic lib give: 1
Dynamic lib add: 1+2=3
Dynamic lib sub: 1-2=-1

I 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.

Checking against C++

g++ -o main main.cpp -ldl
./main

Output (as expected):

3
-1
1

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published