New CMake usage

Hi all,

While I really like having official cmake support for the new ttf, mixer and image releases, I’m struggling to understand how to use it.

Using some SDL2 cmake config I found somewhere, I’d do something like this:

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(MyProject ${SDL2_LIBRARIES})

However, this doesn’t seem to work with the new official cmake configs. Does anyone have any example to do this?

Thanks!

Vendoring SDL/SDL_image/SDL_mixer/SDL_ttf via Git submodules Works For Me™:

cmake_minimum_required( VERSION 3.18.0 )
project( project-name )

set( SDL_TEST OFF CACHE BOOL "" FORCE )
add_subdirectory( external/sdl )

set( BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE )
set( SDL2TTF_HARFBUZZ ON CACHE BOOL "" FORCE )
set( SDL2TTF_SAMPLES OFF CACHE BOOL "" FORCE )
set( SDL2TTF_VENDORED ON CACHE BOOL "" FORCE )
add_subdirectory( external/sdl-ttf )

set( BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_SAMPLES OFF CACHE BOOL "" FORCE )
set( SDL2MIXER_VENDORED ON CACHE BOOL "" FORCE )
add_subdirectory( external/sdl-mixer )

set( BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE )
set( SDL2IMAGE_SAMPLES OFF CACHE BOOL "" FORCE )
set( SDL2IMAGE_VENDORED ON CACHE BOOL "" FORCE )
add_subdirectory( external/sdl-image )

# project setup
add_executable(
    ${CMAKE_PROJECT_NAME}
    "src/main.cpp"
    )
target_link_libraries(
    ${CMAKE_PROJECT_NAME}
    SDL2::SDL2main
    SDL2::SDL2-static
    SDL2_ttf::SDL2_ttf-static
    SDL2_mixer::SDL2_mixer-static
    SDL2_image::SDL2_image-static
    )

Using these commits:

  • SDL: cd2dcf54afaa6d640abf8b83855f6c4b5cda457d
  • SDL_image: 3572a87e97bc7163eda30775880d3eeebd7c54c0
  • SDL_mixer: 3b71dc05d17b11d058ab400df390066cd5ecfba3
  • SDL_ttf: ef1575c0f4213bbe633afe6554d815fc9192af4d
1 Like

Hi
Try this one Cmake sample project
You can update the submodules to the latest versions if you want.

1 Like

Thanks for the answers, but they where a little overkill.

The secret was to use target_include_directories instead of include_directories so that way target_link_libraries can populate the include directories through INTERFACE_INCLUDE_DIRECTORIES once it sees SDL2_image::SDL2_image.

If that didn’t make any sense, I mean something like this:

set(SDL2_IMAGE_DIR "path\\to\\SDL2_image-2.6.0\\cmake")
find_package(SDL2_IMAGE REQUIRED)
target_include_directories(MyProject PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(MyProject SDL2_image::SDL2_image)

I guess Unixes can get away without manually specifying include paths but I’m on Windows at the moment, using CLion.

You don’t need target_include_directories because target_link_libraries allow you to inherit the include_directories property from SDL2_image::SDL2_image

I see, thanks a lot!