Raylib example using the package manager

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.


10 Likes

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

Although both library are present in /usr/lib/x86_64-linux-gnu/. It might be because the path is hardwired here: raylib/src/build.zig at master · raysan5/raylib · GitHub.

Building for x86_64-windows-gnu worked which is one of the thing that always amaze me the most with zig :slight_smile:

1 Like

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.

2 Likes

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.

1 Like

This is very helpful, thanks for the example setup.

Would it be possible to integrate raygui as well?

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.

https://gitlab.pavocracy.dev/personal-projects/raylib-and-raygui-in-zig

1 Like

Hey thanks for the demonstration. Does it support auto-complete by any chance?

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.

2 Likes

I’m seeing those same missing library errors with the latest zig on Ubuntu:

raylib.zig build-lib raylib Debug native: 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

It may be related to this:

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.

I managed to get raygui to work alongside raylib by downloading the single raygui header file.

I added it as a local dependency in the build.zig.zon:

.raygui = .{
            .path = "vendor/raygui", // contains the raygui.h
        },

build.zig:

const raygui_dep = b.dependency("raygui", .{
        .target = target,
        .optimize = raylib_optimize,
    });
    exe.addIncludePath(raygui_dep.path(""));

raylib.zig:

pub usingnamespace @cImport({
    @cInclude("raylib.h");
    @cInclude("raymath.h");
    @cInclude("rlgl.h");
    @cDefine("RAYGUI_IMPLEMENTATION", {});
    @cInclude("raygui.h");
});
2 Likes

Good work, thank you for sharing!

1 Like

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?

Hey, thank you for the notice!

It seems this is related to the recent build system changes, I pushed an updated version.
Can you try if that one works for you?

I tested with current nightly 0.12.0-dev.2618+39ec3d311.

Hi @gabstv - does it still work? I tried to follow your instruction but I get a compile error.

zig: 0.12.0-dev.2154+e5dc9b1d0
raygui: 4.0-28-g7fe39be

zig build
install
└─ install zigraylib
   └─ zig build-exe zigraylib Debug native 1 errors
/home/tom/Documents/code/zigraylib/zig-cache/o/4cdc901eb9bd20a7dbf4afe2d8072796/cimport.zig:11589:101: error: incompatible types: 'c_int' and 'f32'
                                            while (@as(f32, @floatFromInt(j)) < ellipsisWidth) : (j += ellipsisWidth / @as(f32, @floatFromInt(@as(c_int, 3)))) {
                                                                                                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/tom/Documents/code/zigraylib/zig-cache/o/4cdc901eb9bd20a7dbf4afe2d8072796/cimport.zig:11589:99: note: type 'c_int' here
                                            while (@as(f32, @floatFromInt(j)) < ellipsisWidth) : (j += ellipsisWidth / @as(f32, @floatFromInt(@as(c_int, 3)))) {
                                                                                                  ^
/home/tom/Documents/code/zigraylib/zig-cache/o/4cdc901eb9bd20a7dbf4afe2d8072796/cimport.zig:11589:118: note: type 'f32' here
                                            while (@as(f32, @floatFromInt(j)) < ellipsisWidth) : (j += ellipsisWidth / @as(f32, @floatFromInt(@as(c_int, 3)))) {
                                                                                                       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    GuiSliderPro: /home/tom/Documents/code/zigraylib/zig-cache/o/4cdc901eb9bd20a7dbf4afe2d8072796/cimport.zig:12234:9
    GuiSliderBar: /home/tom/Documents/code/zigraylib/zig-cache/o/4cdc901eb9bd20a7dbf4afe2d8072796/cimport.zig:6899:12
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/home/tom/zig/zig build-exe /home/tom/Documents/code/zigraylib/zig-cache/o/946e1d2fe8192c6b57652eaf6145fad3/libraylib.a -fno-strip -ODebug -I /home/tom/Documents/code/zigraylib/zig-cache/i/24f5b46f0d4336f284a49b3d2e6a1496/include -I /home/tom/Documents/code/zigraylib/vendor/raygui --mod root /home/tom/Documents/code/zigraylib/src/main.zig -lGL -lX11 -lc --cache-dir /home/tom/Documents/code/zigraylib/zig-cache --global-cache-dir /home/tom/.cache/zig --name zigraylib --listen=- 
Build Summary: 5/8 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install zigraylib transitive failure
   └─ zig build-exe zigraylib Debug native 1 errors
error: the following build command failed with exit code 1:
/home/tom/Documents/code/zigraylib/zig-cache/o/0b928f27d0b40a4453a40e5300835809/build /home/tom/zig/zig /home/tom/Documents/code/zigraylib /home/tom/Documents/code/zigraylib/zig-cache /home/tom/.cache/zig --seed 0x94d80bb4

Not sure how to interpret it! Is it zig not properly generating FFI code?

EDIT: nevermind, I tried with zig 0.12.0-dev.2641+ee36131e6 and I still get an error, but a different one:

install
└─ install zigraylib
   └─ zig build-exe zigraylib Debug native 1 errors
/home/tom/Documents/code/zigraylib/zig-cache/o/5d09e4975332b67deed4d7fcb13b74fd/cimport.zig:11066:36: error: expected type 'usize', found '[*c]u8'
                    fileDataPtr += @as([*c]u8, @ptrFromInt(@sizeOf(c_int)));
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    GuiLoadStyle: /home/tom/Documents/code/zigraylib/zig-cache/o/5d09e4975332b67deed4d7fcb13b74fd/cimport.zig:5134:17
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/home/tom/zig/0.12.0-dev.2641+ee36131e6/files/zig build-exe /home/tom/Documents/code/zigraylib/zig-cache/o/4d5eea62a9fa485c18150e600bd4a18c/libraylib.a -fno-strip -ODebug -I /home/tom/Documents/code/zigraylib/zig-cache/i/5a2bcaa837574eae8bf7f11fb709c3a7/include -I /home/tom/Documents/code/zigraylib/vendor/raygui/src -Mroot=/home/tom/Documents/code/zigraylib/src/main.zig -lGL -lX11 -lc --cache-dir /home/tom/Documents/code/zigraylib/zig-cache --global-cache-dir /home/tom/.cache/zig --name zigraylib --listen=- 
Build Summary: 5/8 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install zigraylib transitive failure
   └─ zig build-exe zigraylib Debug native 1 errors
error: the following build command failed with exit code 1:
/home/tom/Documents/code/zigraylib/zig-cache/o/05ec56e61b7681f5981ff982181fdc8c/build /home/tom/zig/0.12.0-dev.2641+ee36131e6/files/zig /home/tom/Documents/code/zigraylib /home/tom/Documents/code/zigraylib/zig-cache /home/tom/.cache/zig --seed 0xd30602f5 -Z6f769f552e962f72

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.

Used your template to integrate raylib to my new project. Worked very well, thanks!

1 Like

2 posts were split to a new topic: Zls not working with imported raylib

A post was merged into an existing topic: Zls not working with imported raylib

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.

1 Like