Search code examples
static-librariessdl-2static-linking

Linking SDL2 statically without pulling in X11 dynamically


I am using SDL2.0.12 on Ubuntu 20.04. I link statically to SDL2, adding the linker argument:

`sdl2-config --static-libs`

So far, so good.

However, the SDL static link flags pull in a slew of SDL2 dependencies, that are linked dynamically:

$ sdl2-config --static-libs
-lSDL2 -Wl,--no-undefined -lm -ldl -lasound -lm -ldl -lpthread -lpulse-simple -lpulse -lX11 -lXext -lXcursor -lXinerama -lXi -lXrandr -lXss -lXxf86vm -lwayland-egl -lwayland-client -lwayland-cursor -lxkbcommon -lpthread -lrt

Which leaves me with a slew of dynamic dependencies that may not be present on other linux distributions:

readelf -d foo | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libGL.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libSDL2-2.0.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libasound.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libpulse-simple.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libpulse.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libX11.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libXext.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libXcursor.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libXinerama.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libXi.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libXrandr.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libXss.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libXxf86vm.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libwayland-egl.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libwayland-client.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libwayland-cursor.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libxkbcommon.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

How can I get rid of these dynamic dependencies?

For starters, how could I link statically against X11?


Solution

  • The SDL development libraries provided in Ubuntu have many dynamic dependencies. Even for the static libSDL2.a library.

    When building SDL2 from source, you can avoid these.

    For instance, to avoid a dependency on X11, you would feed it these autoconf flags:

    ./configure \
            --enable-x11-shared \
            --enable-video-x11 \
    

    The --enable-x11-shared will actually make SDL2 link in that dependency at run-time. And not, as you may expect, link against X11 .so dependencies.