SDL2 pulls lots of dependencies

I was really surprised how much memory my simple 3D (openGL) program is using so I looked at pmap of my process and found a lot of unneeded libraries loaded

00007fe958000000   65536      20      20 rw-s- memfd:pulseaudio (deleted)
00007fe95c000000   65536       0       0 rw-s- memfd:pulseaudio (deleted)
00007fe960000000   65536       0       0 rw-s- memfd:pulseaudio (deleted)
00007fe964000000     132       4       4 rw---   [ anon ]
00007fe964021000   65404       0       0 -----   [ anon ]
00007fe968000000   65536       0       0 rw-s- memfd:pulseaudio (deleted)
00007fe96c000000     132       4       4 rw---   [ anon ]
00007fe9947cb000      16      16       0 r---- libvorbis.so.0.4.9
00007fe9947cf000      88      64       0 r-x-- libvorbis.so.0.4.9
00007fe9947e5000      68       0       0 r---- libvorbis.so.0.4.9
00007fe9947f6000       4       0       0 ----- libvorbis.so.0.4.9
00007fe9947f7000       4       4       4 r---- libvorbis.so.0.4.9
00007fe9947f8000       4       4       4 rw--- libvorbis.so.0.4.9
00007fe9947f9000       4       0       0 -----   [ anon ]
00007fe9ac010000      12      12       0 r---- libopus.so.0.8.0
00007fe9ac013000     276      60       0 r-x-- libopus.so.0.8.0
00007fe9ac058000      76       0       0 r---- libopus.so.0.8.0
00007fe9ac06b000       4       0       0 ----- libopus.so.0.8.0
00007fe9ac06c000       4       4       4 r---- libopus.so.0.8.0
00007fe9ac06d000       4       4       4 rw--- libopus.so.0.8.0
00007fe9ac06e000      36      36       0 r---- libFLAC.so.8.3.0
00007fe9ac077000     164      64       0 r-x-- libFLAC.so.8.3.0
00007fe9ac0a0000      40       0       0 r---- libFLAC.so.8.3.0
00007fe9ac0aa000       4       0       0 ----- libFLAC.so.8.3.0
00007fe9ac0ab000       4       4       4 r---- libFLAC.so.8.3.0
00007fe9ac0ac000       4       4       4 rw--- libFLAC.so.8.3.0
00007fe9ac0ad000      80      80       0 r---- libvorbisenc.so.2.0.12
00007fe9ac0c1000      12      12       0 r-x-- libvorbisenc.so.2.0.12
00007fe9ac0c4000     476       0       0 r---- libvorbisenc.so.2.0.12
00007fe9ac13b000     112     112     112 r---- libvorbisenc.so.2.0.12
00007fe9ac157000       4       4       4 rw--- libvorbisenc.so.2.0.12
00007fe9ac158000      28      28       0 r---- libsndfile.so.1.0.31
00007fe9ac15f000     344      64       0 r-x-- libsndfile.so.1.0.31
00007fe9ac1b5000     124       0       0 r---- libsndfile.so.1.0.31
00007fe9ac1d4000      12      12      12 r---- libsndfile.so.1.0.31
00007fe9ac1d7000       4       4       4 rw--- libsndfile.so.1.0.31
00007fe9ac1d8000       8       0       0 rw---   [ anon ]
00007fe9ac1da000      84      84       0 r---- libpulsecommon-15.0.so
00007fe9ac1ef000     288     288       0 r-x-- libpulsecommon-15.0.so
00007fe9ac237000     160     100       0 r---- libpulsecommon-15.0.so
00007fe9ac25f000      12      12      12 r---- libpulsecommon-15.0.so
00007fe9ac262000       4       4       4 rw--- libpulsecommon-15.0.so
00007fe9ac263000      52      52       0 r---- libpulse.so.0.24.0
00007fe9ac270000     188     188       0 r-x-- libpulse.so.0.24.0
00007fe9ac29f000      88      64       0 r---- libpulse.so.0.24.0
00007fe9ac2b5000       8       8       8 r---- libpulse.so.0.24.0
00007fe9ac2b7000       4       4       4 rw--- libpulse.so.0.24.0
00007fe9ac2b8000    2048       0       0 rw-s- renderD128
00007fe9ac4b8000    2048       0       0 rw-s- renderD128
00007fe9ac6b8000       4       0       0 -----   [ anon ]
00007fe9b68aa000      84      84       0 r---- libvulkan.so.1.2.203
00007fe9b68bf000     248     128       0 r-x-- libvulkan.so.1.2.203
00007fe9b68fd000     128      64       0 r---- libvulkan.so.1.2.203
00007fe9b691d000      12      12      12 r---- libvulkan.so.1.2.203
00007fe9b6920000       4       4       4 rw--- libvulkan.so.1.2.203
00007fe9b6921000       8       8       0 r---- libdrm_nouveau.so.2.0.0
00007fe9b6923000      16      16       0 r-x-- libdrm_nouveau.so.2.0.0
00007fe9b6927000       8       0       0 r---- libdrm_nouveau.so.2.0.0
00007fe9b6929000       4       4       4 r---- libdrm_nouveau.so.2.0.0
00007fe9b692a000       4       4       4 rw--- libdrm_nouveau.so.2.0.0

This is just a snippet, but as you can see I have a lot of audio related libs (even though I don’t have any audio yet) and even a vulkan library. Why is that ? Is there a way to prevent that ?
Here’s ldd output just in case

	linux-vdso.so.1 (0x00007ffcf01a5000)
	libSDL2-2.0.so.0 => /usr/lib/libSDL2-2.0.so.0 (0x00007f0b595b7000)
	libm.so.6 => /usr/lib/libm.so.6 (0x00007f0b59473000)
	libGL.so.1 => /usr/lib/libGL.so.1 (0x00007f0b593ed000)
	libc.so.6 => /usr/lib/libc.so.6 (0x00007f0b59221000)
	libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f0b5921a000)
	libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f0b591f9000)
	/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f0b598de000)
	libGLdispatch.so.0 => /usr/lib/libGLdispatch.so.0 (0x00007f0b5913f000)
	libGLX.so.0 => /usr/lib/libGLX.so.0 (0x00007f0b5910c000)
	libX11.so.6 => /usr/lib/libX11.so.6 (0x00007f0b58fca000)
	libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00007f0b58fa0000)
	libXau.so.6 => /usr/lib/libXau.so.6 (0x00007f0b58f9b000)
	libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x00007f0b58f93000)

I could only reproduce this when I used SDL_INIT_AUDIO flag so if you’re not using audio yet don’t pass the flag to SDL_Init.

SDL uses libpulse for audio output, I think the other audio libs get pulled in by libpulse (possibly via libpulsecommon)

So is there no way to avoid it even if I don’t want to use these formats, for example FLAC or ogg ? Also, what’s up with the vulkan and libdrm_nouveau ? I have AMD card so why would i have nouveau loaded ?

Also, since this library pulls in GLX.so (my compilation flags below) how do I use openGL with SDL2 on Wayland ? Do I need to build another binary for Wayland ? I just noticed it now.

LIBS="\
    -lcimgui
    -L../cimgui/
    -lSDL2 \
    -lm \
    -lGL \
"

So is there no way to avoid it even if I don’t want to use these formats, for example FLAC or ogg ?

No, but this is not an SDL issue but a pulseaudio issue.

Also, what’s up with the vulkan and libdrm_nouveau ? I have AMD card so why would i have nouveau loaded ?

No idea, but this is also is something that certainly isn’t SDL2’s fault (it doesn’t even know about nouveau), but Mesas. Probably libGL(X) or libvulkan or whatever

Do I need to build another binary for Wayland ?

Currently SDL2 uses X11 by default, so on Wayland it uses xwayland.
To actually use the Wayland backend you have to enforce it by setting the environment variable SDL_VIDEODRIVER=wayland.
In the next SDL2 release (or the current git code if you build SDL2 yourself), it will use the native Wayland backend by default if you’re on Wayland.

Is it possible to build one binary that will work both on X11 and Wayland ? Since right now when I just link to -GL i also get GLX

You don’t have to link against libGL at all.
You can use an OpenGL loader like glad with SDL_GL_GetProcAddress - SDL Wiki (call gladLoadGLLoader(SDL_GL_GetProcAddress); after creating the OpenGL context) to get the OpenGL functions without having to link libGL or GLX yourself.

Instead of glad there’s also gl3w which I think could be used with gl3wInit2((GL3WGetProcAddressProc)SDL_GL_GetProcAddress); - most other OpenGL function loaders I looked at (like glew) seem to use glx/wgl directly instead of letting the user provide a function pointer to SDL_GL_GetProcAddress() (or equivalent functions).

1 Like

Thanks ! I will try that.
EDIT: It works. Same binary works both on Wayland and X11 with Glad :slight_smile:

1 Like