Replacing system dependency with a Zig-built one

I’m building C++ sources using Zig. Currently I have system dependencies in build.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)

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

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.

Yes, compiling on Linux for Windows or macOS

Yes

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

You can try to use GitHub - allyourcodebase/SDL: SDL with the build system replaced by Zig

Also related:

1 Like

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

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

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

Then you can add the dependency in your build.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:

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:

Which states that it supports cross compile.

1 Like

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.

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

1 Like

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

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

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 repos and use fetch them with build.zig.zon if SDL depends on them.

2 Likes