静态链接SDL2而不动态拉入X11

Linking SDL2 statically without pulling in X11 dynamically

我在 Ubuntu 20.04 上使用 SDL2.0.12。我 link 静态地添加到 SDL2,添加 linker 参数:

`sdl2-config --static-libs`

到目前为止,还不错。

但是,SDL 静态 link 标志引入了大量 SDL2 依赖项, link动态编辑的:

$ 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

这给我留下了一系列动态依赖,这些依赖可能不存在于其他 linux 发行版中:

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]

如何摆脱这些动态依赖?

对于初学者,我如何link 静态地对抗 X11?

Ubuntu中提供的SDL开发库有很多动态依赖。即使对于静态 libSDL2.a 库。

从源代码构建 SDL2 时,您可以避免这些。

例如,为了避免对 X11 的依赖,您可以为它提供这些 autoconf 标志:

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

--enable-x11-shared 实际上会使 SDL2 link 在 运行 时处于该依赖关系中。而不是,如您所料,link 针对 X11 .so 依赖项。