When a graphics project needs a consistent OpenGL interface across platforms, system headers can be a headache: they may be missing or out of sync with the OpenGL version supported by the system. glad generates an OpenGL loader library for selected versions and extensions, including a consistent header for the chosen API. section 7.1.3 section 7.2.2
GLFW requests OpenGL features and acquires a context; glad supplies the loader interface. For a project targeting OpenGL 3.3, generate the core profile without extensions at gen.glad.sh. The core profile is enough when the project does not need OpenGL 1.x or 2.x functionality. section 7.1.3 section 7.2.2
Vendor the generated files in a glad/ directory alongside src/ and build.zig. Keeping the code in the project avoids depending on its continued availability, lets updates happen in lock-step with project changes, and leaves room for local fixes. section 7.2.2
To expose glad to Zig through src/c.h, include its header before GLFW’s header. GLFW can then detect that glad is in use. section 7.2.2
#include <glad/glad.h> #include <GLFW/glfw3.h>
The include order puts glad before GLFW. section 7.2.2
The C translator also needs the generated header’s include path. Unlike the system-installed GLFW header in this setup, it cannot find glad’s header on its own. section 7.2.2
translator.addIncludePath(b.path("glad/include"));
Add glad/include to the translator’s search paths. section 7.2.2
Finally, compile the generated glad.c as part of the build. Zig’s compiler can compile the C source directly. section 7.2.2
c.addCSourceFile(.{ .file = b.path("glad/src/glad.c") });
Add the generated C source to the build. section 7.2.2
