# Raylib example using the package manager

**URL:** <https://ziggit.dev/t/raylib-example-using-the-package-manager/1787>\
**Category:** Showcase\
**Tags:** package-management, ecosystem, raylib, zig-0-13, zig-0-14, zig-0-15-2\
**Created:** [September 19, 2023, 11:03pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787 "2023-09-19T23:03:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [September 19, 2023, 11:03pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/1 "2023-09-19T23:03:30Z")

</div>

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.

[https://github.com/SimonLSchlee/zigraylib](https://github.com/SimonLSchlee/zigraylib)

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.

* * *

- added [raygui](https://github.com/SimonLSchlee/zigraylib/tree/raygui) branch [added raygui #5 · SimonLSchlee/zigraylib@60b2ed0 · GitHub](https://github.com/SimonLSchlee/zigraylib/commit/60b2ed0af9634421a936308152315c0017194c3b)

* * *

# Project Update:

- moved to Codeberg: [https://codeberg.org/SimonLSchlee/zigraylib](https://codeberg.org/SimonLSchlee/zigraylib)
- Added tags for `zig-0.14.0` and `zig-0.15.2`
- Added support for `-Dtarget=wasm32-emscripten`

I also wanted to add a tag for `zig-0.16.0` but that version has a problem with emscripten builds, so I will likely skip that version and create the next tag for `0.17.0`.

---

<div class="post-metadata">

**Author:** ![Luke](https://ziggit.dev/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@Luke](https://ziggit.dev/u/Luke)\
**Post date:** [September 20, 2023, 6:56am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/2 "2023-09-20T06:56:35Z")

</div>

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:

```zig
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](https://github.com/raysan5/raylib/blob/master/src/build.zig#L84).

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

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [September 20, 2023, 3:07pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/3 "2023-09-20T15:07:58Z")

</div>

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`](https://github.com/ziglang/zig/blob/a63a1c5cb9fd31a57e8371e6cba5f316bd3f2a65/lib/std/zig/system/NativePaths.zig#L104-L129) does it.

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [September 21, 2023, 12:04pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/4 "2023-09-21T12:04:37Z")

</div>

> [@Luke](#):
>
> One remark would be that, on an actual project, I would keep the .optimize flag as is for raylib.

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.

---

<div class="post-metadata">

**Author:** ![lhk](https://ziggit.dev/letter_avatar_proxy/v4/letter/l/65b543/32.png) [@lhk](https://ziggit.dev/u/lhk)\
**Post date:** [October 16, 2023, 8:45pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/5 "2023-10-16T20:45:21Z")

</div>

This is very helpful, thanks for the example setup.

Would it be possible to integrate raygui as well?

---

<div class="post-metadata">

**Author:** ![pavocracy](https://ziggit.dev/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@pavocracy](https://ziggit.dev/u/pavocracy)\
**Post date:** [October 17, 2023, 10:44am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/6 "2023-10-17T10:44:37Z")

</div>

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](https://gitlab.pavocracy.dev/personal-projects/raylib-and-raygui-in-zig)

---

<div class="post-metadata">

**Author:** ![dex](https://ziggit.dev/user_avatar/ziggit.dev/dex/32/1198_2.png) [@dex](https://ziggit.dev/u/dex)\
**Post date:** [October 30, 2023, 3:59am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/7 "2023-10-30T03:59:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [October 30, 2023, 12:02pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/8 "2023-10-30T12:02:09Z")

</div>

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](https://github.com/zigtools/zls/wiki/Installation)

All the credits go to the people doing the awesome work of creating and maintaining [zls](https://github.com/zigtools/zls):

 ![zigraylib-vscode-zigextension-zls](https://ziggit.dev/uploads/default/original/2X/9/9568286ac752d6fe8a8ac966a841bf0fc36a273f.png)

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.

---

<div class="post-metadata">

**Author:** ![buzmeg](https://ziggit.dev/user_avatar/ziggit.dev/buzmeg/32/622_2.png) [@buzmeg](https://ziggit.dev/u/buzmeg)\
**Post date:** [November 23, 2023, 1:49am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/9 "2023-11-23T01:49:51Z")

</div>

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

```zig
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:

> <https://github.com/ziglang/zig/issues/16733>
>
> \### Zig Version
> 
> 0.11.0
> 
> \### Steps to Reproduce and Observed Behavior
> 
> Depend on… something like GTK: \`step.linkSystemLibrary("gtk4")\`. On Xubuntu, gtk4 from apt is installed in \`/usr/lib/aarch64-linux-gnu\`. I've noticed this on other distros as well such as the flatpak build environment.
> 
> \### Expected Behavior
> 
> Given this is a common path for default packages across distros, I think NativePaths should add it here:
> 
> https://github.com/ziglang/zig/blob/d0fd67cffe664ff70d9a70dd4d2d28aba5a378e8/lib/std/zig/system/NativePaths.zig#L129
> 
> We already add \`/lib/{linux triple}\`.

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [November 23, 2023, 2:34am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/10 "2023-11-23T02:34:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gabstv](https://ziggit.dev/user_avatar/ziggit.dev/gabstv/32/903_2.png) [@gabstv](https://ziggit.dev/u/gabstv)\
**Post date:** [December 2, 2023, 2:45am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/11 "2023-12-02T02:45:38Z")

</div>

> [@lhk](#):
>
> raygui

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:

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

```

build.zig:

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

```

raylib.zig:

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

```

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [December 2, 2023, 3:07am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/12 "2023-12-02T03:07:07Z")

</div>

Good work, thank you for sharing!

---

<div class="post-metadata">

**Author:** ![tgirod](https://ziggit.dev/user_avatar/ziggit.dev/tgirod/32/3744_2.png) [@tgirod](https://ziggit.dev/u/tgirod)\
**Post date:** [February 6, 2024, 9:01pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/13 "2024-02-06T21:01:00Z")

</div>

hey there! I’ve just tried this project, but got a compilation error with zig 0.12.0-dev.2154+e5dc9b1d0

```zig
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?

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 6, 2024, 9:33pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/14 "2024-02-06T21:33:43Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![tgirod](https://ziggit.dev/user_avatar/ziggit.dev/tgirod/32/3744_2.png) [@tgirod](https://ziggit.dev/u/tgirod)\
**Post date:** [February 8, 2024, 8:16am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/15 "2024-02-08T08:16:37Z")

</div>

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
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:

```zig
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

```

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [February 8, 2024, 9:23am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/16 "2024-02-08T09:23:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AndreasHefti](https://ziggit.dev/user_avatar/ziggit.dev/andreashefti/32/1887_2.png) [@AndreasHefti](https://ziggit.dev/u/AndreasHefti)\
**Post date:** [March 1, 2024, 7:24pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/17 "2024-03-01T19:24:32Z")

</div>

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

> **[GitHub - AndreasHefti/firefly-zig: Firefly is a 2D Game API and engine...](https://github.com/AndreasHefti/firefly-zig)**
>
> Firefly is a 2D Game API and engine strongly based on ECS design. Uses Raylib under the hood - AndreasHefti/firefly-zig

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [April 17, 2024, 8:17am UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/18 "2024-04-17T08:17:53Z")

</div>

2 posts were split to a new topic: [Zls not working with imported raylib](https://ziggit.dev/t/zls-not-working-with-imported-raylib/3986)

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [April 17, 2024, 1:22pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/19 "2024-04-17T13:22:47Z")

</div>

A post was merged into an existing topic: [Zls not working with imported raylib](https://ziggit.dev/t/zls-not-working-with-imported-raylib/3986/4)

---

<div class="post-metadata">

**Author:** ![ngrilly](https://ziggit.dev/user_avatar/ziggit.dev/ngrilly/32/837_2.png) [@ngrilly](https://ziggit.dev/u/ngrilly)\
**Post date:** [April 17, 2024, 3:24pm UTC](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787/20 "2024-04-17T15:24:39Z")

</div>

I’m toying [here](https://github.com/ngrilly/zig3d) 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.

[Next page](https://ziggit.dev/t/raylib-example-using-the-package-manager/1787.md?page=2)
