Here is a fairly minimal example that uses raylib with the zig package manager.
I might add some more links/resources or maybe other examples as branches some other time.
Currently it is created with only a few modifications from the default zig init-exe project,
to make it easier to see what steps were required.
Thanks for that! A great starting point when wanting to use raylib indeed.
One remark would be that, on an actual project, I would keep the .optimize flag as is for raylib. You might want to build to ReleaseSmall for example. I tried BTW and got to 800K, so not bad.
As a zig noob I tried experimenting and got a weird error. I tried to compile with -Dtarget=x86_64-linux-gnu. Don’t ask me why, just playing around. And I got a failure:
raylib.zig build-lib raylib Debug x86_64-linux-gnu: error: error: unable to find Dynamic system library 'GL' using strategy 'paths_first'. searched paths:
/usr/lib/libGL.so
/usr/lib/libGL.a
error: unable to find Dynamic system library 'X11' using strategy 'paths_first'. searched paths:
/usr/lib/libX11.so
/usr/lib/libX11.a
I could be wrong but I believe that by default Zig only looks for system headers/libraries when compiling for native targets. It won’t look for them when you specify a target like -Dtarget=x86_64-linux-gnu even if your machine happens to be the same target.
If the raylib build.zig wants to look for system headers/libs when targeting any Linux target it might want to try to replicate how std.zig.system.NativePaths.detect does it.
I removed the hard coded raylib override with comments and instead replaced it with options and some hints printed to console on exit.
Now you can use zig build -Doptimize=ReleaseSmall -Dstrip=true for a small stripped build like you did, or you can use zig build -Draylib-optimize=ReleaseFast for a debug build of your own app that uses a release build of raylib (if you aren’t debugging raylib). The raylib-optimize defaults to the value of optimize when its not provided.
Instead of creating a new thread under the showcase category, i might just post to the end of here instead. As lhk and I have been troubleshooting elsewhere, I’ve managed to get raygui to work in zig along side raylib. It uses the package manager but manages the linking manually since raygui doesn’t have a build.zig file.
I updated the example to the newest nightly/master for both zig and raylib.
I also added a link to the zls wiki that describes how to install zls to get completions / language server support: Installation · zigtools/zls Wiki · GitHub
All the credits go to the people doing the awesome work of creating and maintaining zls:
This is just with installing the vscode extension from the link above and letting it install its own zig and zls (newest master).
My neovim config currently doesn’t work properly, but I think that is something that is broken about my personal config, I need to get around to fix that.
I am not quite sure, but I think the problem might be that the raylib build.zig file does not use the triple, I think patching that like @castholm suggested within raylib might fix it.
hey there! I’ve just tried this project, but got a compilation error with zig 0.12.0-dev.2154+e5dc9b1d0
zig build run
/home/tom/Documents/code/zigraylib/build.zig:38:9: error: no field named 'strip' in struct 'Build.Step.Compile'
exe.strip = strip;
^~~~~
/home/tom/zig/lib/std/Build/Step/Compile.zig:1:1: note: struct declared here
const builtin = @import("builtin");
^~~~~
referenced by:
runBuild__anon_8152: /home/tom/zig/lib/std/Build.zig:1858:27
steps__anon_7975: /home/tom/zig/lib/build_runner.zig:1044:29
remaining reference traces hidden; use '-freference-trace' to see all reference traces
maybe there has been changes in zig build since the last update?
Your version of raygui is too old, try with the latest master of raygui.
With the most recent update zigraylib uses raylib 5-dev, so you need the most recent raygui.
Because build system apis are still changing and we are using raylibs build.zig we are chasing the newest versions, so that zig and raylib can work together.
If that changes we can pin some versions, but I don’t know when that will happen.
I’m toying here with Zig and raylib and converged to the exact same pattern as the one in your repo. It’s really amazing to be able to let Zig fetch and build raylib, with no additional tool, and no need for a Zig wrapper around the C library. And zls auto-completion works on raylib functions and types as well.