Is this possible to use a C external library into a build.zig?

Hi,

I am working on a Vulkan app. I am trying from my build.zig to compile shaders I wrote. I already know that I can achieve this by calling glslc with std.Build.Step.evalChildProcess() but I want to use the libshaderc to do this. So I already made a fork of the google/shaderc library where I replaced the cmake build system with zig:

So now I can use my shaderc.zig as a dependency into my build.zig. I can link it to my app. However that is not really what I want: I can compile my shaders using libshaderc during the run time but I would like to avoid this if possible. I want to compile my shaders during compile time. I do not have any idea how to use shaderc.zig to use C functions from it into my build.zig. I made some research but the topics I found did not help me to do this. Can someone show me a minimal example or tell me if it is possible ?

Thank you.

see this thread: Can I use packages inside build.zig?

3 Likes

Hi,

First of all, thank you for your answer. Maybe I am missing something but with your link I only know how to share Zig functions between a Zig repository and the build.zig of another repository.
The other thing I need to know is how to translate C code to Zig during compile time to use it in my build.zig ?

Let say I have this C function in a C file awesome.c:

#include "awesome.h"

void my_awesome_c_function ()
{
  printf ("This function makes complex things!");
} 

and the matching header file awesome.h:

#include <stdio.h>

void my_awesome_c_function ();

How can I use Zig translation during compile time to use my_awesome_c_function also during compile time (so into build.zig) ? Is this possible ?

I found a solution thanks to this repository: GitHub - zkburke/quanta: A game engine/framework written in and for zig

The author of this repository uses two independent executables into his/her build.zig. A first one is installed and executed before the second one. The first executable uses the C extern library to compile shaders and the second one (the app which uses the compiled shaders) is built and executed in a second time. At least shaders are not compiled during the runtime of the main app.

1 Like