I’m a newcomer to Zig, but I met Andrew Kelly and several other Zig developers at Handmade Seattle this week, and it motivated me to start experimenting with the language.
I had a hard time finding simple, complete documentation about how to call C code from Zig. I pieced together a solution from a few different sources and then documented what I did:
Great article! I think this will be helpful to other newcomers.
There is one small detail that I’d like to add:
This article showed the simplest example I could think of for showing how to call C code from Zig.
There actually is an even simpler way. You don’t need to add a static library, you can also directly add the c code to the executable:
exe.linkLibC();
exe.addCSourceFiles(...);
But you’d obviously need to this for the unit test step as well.
So if you use unit-testing(and the tests require the C library), making a static lib is the better solution I guess.
Thanks @IntegratedQuantum and @bortzmeyer! Including the .c file directly simplifies things a lot. I’ve updated the post and credited you both for the help!