# Replacing system dependency with a Zig-built one

**URL:** <https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240>\
**Category:** Help\
**Tags:** build-system\
**Created:** [January 29, 2025, 10:07am UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240 "2025-01-29T10:07:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![faveoled](https://ziggit.dev/user_avatar/ziggit.dev/faveoled/32/4126_2.png) [@faveoled](https://ziggit.dev/u/faveoled)\
**Post date:** [January 29, 2025, 10:07am UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/1 "2025-01-29T10:07:36Z")

</div>

I’m building C++ sources using Zig. Currently I have system dependencies in build.zig:

```zig
    ziggame.linkSystemLibrary("SDL2");
    ziggame.linkSystemLibrary("SDL2_mixer");
    ziggame.linkSystemLibrary("GL");

```

What should my next steps be to switch to building them using Zig? (I want to be able to cross-compile for Windows, currently it’s failing with the error below)

```zig
error: error: unable to find dynamic system library 'SDL2' using strategy 'paths_first'. searched paths: none
error: unable to find dynamic system library 'SDL2_mixer' using strategy 'paths_first'. searched paths: none
error: unable to find dynamic system library 'GL' using strategy 'paths_first'. searched paths: none

```

---

<div class="post-metadata">

**Author:** ![tsdtas](https://ziggit.dev/user_avatar/ziggit.dev/tsdtas/32/3512_2.png) [@tsdtas](https://ziggit.dev/u/tsdtas)\
**Post date:** [January 29, 2025, 2:31pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/2 "2025-01-29T14:31:01Z")

</div>

Are you switching from building natively for your own (Linux? Mac?) machine to cross-compiling for Windows? Did you install the SDL/libGL with a package manager?

`searched paths: none` indicates that Zig didn’t look for the library anywhere.

When cross-compiling, Zig won’t look in your system for installed libraries, because the libraries on your machine probably won’t work on the machine you’re compiling for.

Instead you need to get a copy of the library for Windows and then specify the location of the library file. There’s a few different ways to do that depending on what you need, but the simplest is probably with `std.Build.addLibraryPath` pointing to a folder with the libraries.

---

<div class="post-metadata">

**Author:** ![faveoled](https://ziggit.dev/user_avatar/ziggit.dev/faveoled/32/4126_2.png) [@faveoled](https://ziggit.dev/u/faveoled)\
**Post date:** [January 29, 2025, 3:45pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/3 "2025-01-29T15:45:27Z")

</div>

> [@tsdtas](#):
>
> Are you switching from building natively for your own (Linux? Mac?) machine to cross-compiling for Windows?

Yes, compiling on Linux for Windows or macOS

> [@tsdtas](#):
>
> Did you install the SDL/libGL with a package manager?

Yes

> [@tsdtas](#):
>
> Instead you need to get a copy of the library for Windows and then specify the location of the library file

I probably won’t find the binaries needed for different architectures of different OSes, I think I need some kind of a source-based solution

---

<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:** [January 29, 2025, 3:55pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/4 "2025-01-29T15:55:16Z")

</div>

You can try to use [GitHub - allyourcodebase/SDL: SDL with the build system replaced by Zig](https://github.com/allyourcodebase/SDL)

Also related:

> [@Build allyourcodebase/SDL on linux](https://ziggit.dev/t/build-allyourcodebase-sdl-on-linux/6857):
>
> When using [GitHub - allyourcodebase/SDL: SDL with the build system replaced by Zig](https://github.com/allyourcodebase/SDL) on linux in my build, I get a whole lot of errors like this: ... SDL\_config.h.cmake: 540 unable to substitute variable: error: MissingValue Anyone knows how can I fix this? What am I missing?

---

<div class="post-metadata">

**Author:** ![faveoled](https://ziggit.dev/user_avatar/ziggit.dev/faveoled/32/4126_2.png) [@faveoled](https://ziggit.dev/u/faveoled)\
**Post date:** [January 29, 2025, 3:59pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/5 "2025-01-29T15:59:38Z")

</div>

> [@Sze](#):
>
> You can try to use [GitHub - allyourcodebase/SDL: SDL with the build system replaced by Zig](https://github.com/allyourcodebase/SDL)

This looks good, but what would be the proper way to depend on it in by build?

---

<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:** [January 29, 2025, 5:04pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/6 "2025-01-29T17:04:44Z")

</div>

_Unfortunately this package relies on system libraries so you won’t be able to use it to cross compile your application._  
(unless there is some way to do the build without the system libraries or supply the needed parts, I am not entirely sure, for pure dynamic libraries it may work, but if there are also headers needed at build time it would probably fail)

* * *

I haven’t used this package in particular, but usually you start with adding it as a dependency to your project with:

```zig
zig fetch --save git+https://github.com/allyourcodebase/SDL

```

which fetches the dependency and adds this to your `build.zig.zon` (commit/hash may differ once the branch has been updated):

```zig
.SDL = .{
    .url = "git+https://github.com/allyourcodebase/SDL#a13d7e80e8d847db2b2ff8f334d57eb5bbb489dd",
    .hash = "12200aa79b05aaeefff144b9e376371e2e7ddc982b9207d146163baf56361331a834",
},

```

Then you can add the dependency in your `build.zig`:

```zig
const sdl_dep = b.dependency("SDL", .{
    .target = target,
    .optimize = optimize,
});
const sdl_lib_artifact = sdl_dep.artifact("SDL2");
exe.linkLibrary(sdl_lib_artifact);

```

On linux I get with that:

```zig
SDL_opengles.h:33:10: error: 'GLES/gl.h' file not found

```

So maybe it relies on system headers for that.

* * *

I just saw that the `build.zig` uses linkSystemLibrary for window / mac etc., so that ~~won’t work~~ may not unless you compile on windows. (not sure)

I think (but I am not sure that) if you want cross compile to work you would have do a lot of work on the SDL2 library to make it support cross compile (remove/bundle all the system dependencies), would probably be way easier to get different compiled libraries like @tsdtas has suggested.

Alternatively you could port your application to use SDL3 and this project:

> [@SDL3 ported to the Zig build system (with example games)](https://ziggit.dev/t/sdl3-ported-to-the-zig-build-system-with-example-games/7606):
>
> 2024 is drawing its last breath and just in time for the new year, I’ve completed my port of [SDL3](https://github.com/libsdl-org/SDL) to the Zig build system. SDL3 is still in preview (current version: 3.1.6), but its ABI is stable as of 3.1.3 and it has been used in production by Valve for some time now. The goal for this package was an SDL build that Just Works™ with as few prerequisites and caveats as possible. To my knowledge, this is the first port of SDL that simultaneously supports Zig 0.12.1, 0.13.0 and 0.14.0-dev, bu…

Which states that it supports cross compile.

---

<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:** [January 29, 2025, 5:37pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/7 "2025-01-29T17:37:21Z")

</div>

Thinking more about it I am not sure whether cross compile works or not.  
I haven’t done a lot of cross compiling maybe it could be made to work.

Because I am not sure I am removing this as accepted answer and hoping that somebody with more experience with cross compile can answer.

---

<div class="post-metadata">

**Author:** ![faveoled](https://ziggit.dev/user_avatar/ziggit.dev/faveoled/32/4126_2.png) [@faveoled](https://ziggit.dev/u/faveoled)\
**Post date:** [January 29, 2025, 5:43pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/8 "2025-01-29T17:43:48Z")

</div>

The approach is clear now, whether or not it cross-compiles is a different issue altogether

---

<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:** [January 29, 2025, 5:46pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/9 "2025-01-29T17:46:06Z")

</div>

Hmm okay then I guess we can accept it as answer and let the cross compile stuff be a question for later.

---

<div class="post-metadata">

**Author:** ![D-Berg](https://ziggit.dev/user_avatar/ziggit.dev/d-berg/32/4195_2.png) [@D-Berg](https://ziggit.dev/u/D-Berg)\
**Post date:** [January 29, 2025, 6:26pm UTC](https://ziggit.dev/t/replacing-system-dependency-with-a-zig-built-one/8240/10 "2025-01-29T18:26:35Z")

</div>

One can use linksystemlibrary for windows libraries and cross compile from other platforms. I do it like this [zgl/build.zig at 523e1ddd077ae267c274560779e20c0dbb8ef568 · D-Berg/zgl · GitHub](https://github.com/D-Berg/zgl/blob/523e1ddd077ae267c274560779e20c0dbb8ef568/build.zig#L246)

Compile, check missing symbols, google which windows lib it’s in and link it.

You can search the zig repo for the windows functions you need and check the library name also.

It also works for macOS system libs. For Linux x11 and wayland headers you can use [Hexops · GitHub](https://github.com/hexops) repos and use fetch them with build.zig.zon if SDL depends on them.
