#Invoking Zig in other Languages

1 messages · Page 1 of 1 (latest)

hard canyon
#

Looking for documentation. Can anybody point me in the right direction regarding linking Zig static library -> .lib, .dll, .so files to be used by other programming languages via bindings/FFI?

hallow sparrow
#

export the functions, write a header/main medium of interaction of the language of choice

#

in C# for example would be:

zig code:

export add(a: u32, b:u32) u32 {return a +% b;}

compile this with zig build-lib -dynamic mathlib.zig

    [DllImport("libmathlib.so", CallingConvention = CallingConvention.Cdecl)]
    public static extern uint add(uint a, uint b);

And now you got interaction

#

other languages might need a header file so you would have to write it yourself of hope that using the header generation code in zig works 😛

#
using System.Runtime.InteropServices;

[DllImport("libmathlib.so", CallingConvention = CallingConvention.Cdecl)]
public static extern uint add(uint a, uint b);

System.Console.WriteLine(add(100,200));

is the full program of the example, supposing they are in the same folder