24 December 2024
For my December Adventure I had to figure out how to use the Raylib library with Zig. It’s not difficult but it’s also not well documented, so I decided to write up the steps here.
I’ve tested this with Zig 0.14 and Raylib 5.5 under Linux. I’m assuming Zig is already installed;
otherwise the only dependencies should be standard Linux utilities such as tar
.
Go to the Raylib release page and download
the “source” .tar.gz file. It includes a Zig build file, so we can build it with
zig build
:
tar xf raylib-5.5.tar.gz
cd raylib-5.5
zig build --release=fast
This puts the output in zig-out
:
raylib-5.5$ tree zig-out/
zig-out/
├── include
│ ├── raylib.h
│ ├── raymath.h
│ └── rlgl.h
└── lib
└── libraylib.a
We’ll install it by just copying the files:
sudo cp zig-out/include/*.h /usr/local/include/
sudo cp zig-out/lib/libraylib.a /usr/local/lib/
To use Raylib in Zig code, import the header file as follows:
const ray = @cImport({
@cInclude("raylib.h");
});
Now Raylib’s types, functions, and constants are available with the ray.
prefix, for example
ray.BLACK
and ray.DrawTriangle
.
To build the code, we have to tell Zig to link with libc and raylib. If you’re running
zig
directly, add -lc -lraylib
to the command line. If you use a
build.zig
file, add the following:
exe.linkSystemLibrary("raylib");
exe.linkLibC();
For full sample code, see build.zig and triangle.zig. It shows… a triangle: