I include SDL2 in the project in the following way:
cmake_minimum_required(VERSION 3.26)
project(game)
set(BUILD_SHARED_LIBS OFF)
set(SDL2_DISABLE_INSTALL ON)
set(SDL_TEST OFF)
add_subdirectory(external/SDL)
set(SDL2IMAGE_VENDORED ON)
set(SDL2IMAGE_INSTALL OFF)
set(SDL2IMAGE_SAMPLES OFF)
add_subdirectory(external/SDL_image)
set(SDL2MIXER_VENDORED ON)
set(SDL2MIXER_INSTALL OFF)
set(SDL2MIXER_SAMPLES OFF)
add_subdirectory(external/SDL_mixer)
file(GLOB_RECURSE GAME_SRC CONFIGURE_DEPENDS "include/*.h" "src/*.cpp")
add_executable(game ${GAME_SRC})
if(TARGET SDL2::SDL2main)
target_link_libraries(game PRIVATE SDL2::SDL2main)
endif()
target_link_libraries(game PRIVATE SDL2::SDL2-static SDL2_image::SDL2_image-static SDL2_mixer::SDL2_mixer-static)
target_link_options(game PRIVATE -static-libgcc -static-libstdc++)
It compiles and works fine. Howewer, such libraries as libm
, libc
, or libwinpthread-1.dll
on Windows are still being dynamically linked. If I add -static
to linker options, the following warning will appear while building:
/usr/bin/ld: external/SDL/libSDL2.a(SDL_dynapi.c.o): in function «SDL_InitDynamicAPI»:
SDL_dynapi.c:(.text+0x41be): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
If I run built executable, it will crash with segmentation fault. What is the proper way to build static SDL2 application?
UPD: this problem doesn’t appear on Windows.