How does SDL_GL_LoadLibrary find library from the system?

I’m trying to debug some open source game to make them works properly inside NixOS.

At first, I thought that this function would attempt to find library from the executable’s RPATH but that’s seems to not be the case.

Here’s what I have so far:

After a build, in the CMakeCache file I see this line: build/CMakeCache.txt:OPENGL_opengl_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libOpenGL.so
It looks like this might be auto-generated from SDL/cmake/sdlchecks.cmake from this line: FindOpenGLHeaders(), but I don’t know cmake at all, so that’s about as far as I go there.

I don’t know how the above is actually used.

Once you get into the source code, it looks like they have some common names for the libs like this:

// Desktop Linux/Unix-like
#define DEFAULT_OGL        "libGL.so.1"
// ...
#define ALT_OGL            "libOpenGL.so.0"

Then they use SDL_LoadObject(); to load the path:

#ifdef DEFAULT_OGL
            if (!opengl_dll_handle) {
                path = DEFAULT_OGL;
                opengl_dll_handle = SDL_LoadObject(path);
            }
#endif

If the default fails, then they try the alt name.
All the above to say that finding the lib is left to the dynamic linker since the library name is all that is passed to it. (My understanding is that there are multiple system paths that the dynamic linker can search until the list is exhausted or the lib is found. I don’t know the order taken in that search, but you can find out.)

Thanks a lot. After reading through bunch of Cmake, I found that SDL actually has SDL_RPATH option which was disabled in the project, I have re-enabled it and now they’re properly reading from RPATH now. Also, seems like ldconfig is not a thing in NixOS.

1 Like