I tried to import SDL2 with cmake, but got a lot of errors


The compiler I chose to use is mingw64, and I am not familiar with cmake. I haven’t changed any of the cmake files that come with SDL2,Is this because my CMakeLists.txt is written incorrectly? thank

2.30.10 has broken MinGW CMake support, try 2.30.9 instead.

1 Like

Look at CPM.cmake for dependencies. I’ve started using it recently and it’s a game changer. I am able to pull down the preview version of SDL3 and compile and link it statically with very little code:

cmake_minimum_required(VERSION 3.30)
project(sdltest)

set(CMAKE_CXX_STANDARD 20)

include(cmake/CPM.cmake)

# In order to get the hash:
# wget https://github.com/libsdl-org/sdl/archive/refs/tags/preview-3.1.6.tar.gz -O - | sha256sum
CPMAddPackage(
    NAME sdl
    URL https://github.com/libsdl-org/sdl/archive/refs/tags/preview-3.1.6.tar.gz
    URL_HASH SHA256=5da5e265c150b954d007bf1465b155d9df1d0d52f10115a49bb918dc8fe2826a
    OPTIONS "SDL_SHARED OFF" "SDL_STATIC ON"
)

CPMAddPackage("gh:g-truc/glm#1.0.1")

# See `README.md`.
find_program(SHADERCROSS
    NAMES shadercross
    PATHS /usr/local/bin
    REQUIRED
)

add_executable(sdltest
    src/common.hpp
    src/main.cpp
    src/appstate.hpp
    src/appstate.cpp
    src/game.hpp
    src/game.cpp
    src/pipeline.hpp
    src/pipeline.cpp
)

target_link_libraries(sdltest
    SDL3::SDL3
    glm::glm
)

add_custom_target(shaders)

file(GLOB SHADER_FILES res_src/shaders/*.hlsl)
foreach (FILE ${SHADER_FILES})
    get_filename_component(BASE_NAME ${FILE} NAME_WLE)
    set(OUT_FILE "${CMAKE_SOURCE_DIR}/res/shaders/${BASE_NAME}")
    if (APPLE)
        set(SHADER_FORMAT "MSL")
    else ()
        message(FATAL_ERROR "Unsupported platform")
    endif (APPLE)
    add_custom_command(
        TARGET shaders
        COMMAND ${SHADERCROSS} -d ${SHADER_FORMAT} -o ${OUT_FILE} ${FILE}
        MAIN_DEPENDENCY ${FILE}
    )
endforeach ()

add_dependencies(sdltest shaders)

Did 2.30.11 fix it back?

There are no more absolute paths in the sdl2-config.cmake files or 2.30.11, so they should be fine.

This hick-up was caused by the libsdl-org project automating the release process.