CMake superbuild with SDL2 and SDL_image/ttf/mixer

Hi all,

I’m working on a cross-platform SDL2 project that I want to be able to compile for PC, Mac, Linux, Browser (Emscripten), iOS, and Android.

A few months back, I got it all to work by setting up the build directories and projects manually (and verrry carefully). You can see my extremely hacky setup to get it working here: https://github.com/lwb4/tadpole

Now, I want to redo the build system so it all works with cmake. My approach is to include full source for all dependencies as git submodules, and since most of them are cmake projects, I can just add_subdirectory to tie it all together.

Well, not really. There’s one small hitch: SDL_ttf and SDL_image won’t build. SDL2 and SDL_mixer appear to be working however.

This is roughly what my top-level CMakeLists.txt file looks like:

add_subdirectory("${SDL2_DIR}")
add_subdirectory("${SDL_MIXER_DIR}")
add_subdirectory("${SDL_IMAGE_DIR}")
add_subdirectory("${SDL_TTF_DIR}")
...
target_include_directories(... "${SDL2_DIR}/include")
target_include_directories(... "${SDL2_DIR}/include" "${SDL_TTF_DIR}" "${SDL_IMAGE_DIR}" "${SDL_MIXER_DIR}/include")
target_link_libraries(Tadpole SDL2)
target_link_libraries(Tadpole SDL_mixer)
target_link_libraries(Tadpole SDL_ttf)
target_link_libraries(Tadpole SDL_image)

And here’s the error when I mkdir build && cd build && cmake ..:

CMake Error at deps/SDL2/SDL2Config.cmake:1 (include):
  include could not find load file:

    /Users/lincolnbergeson/Pollywog/tadpole/deps/SDL2/SDL2Targets.cmake
Call Stack (most recent call first):
  deps/SDL_image/CMakeLists.txt:7 (find_package)


-- Symbol prefix:
CMake Error at deps/SDL2/SDL2Config.cmake:1 (include):
  include could not find load file:

    /Users/lincolnbergeson/Pollywog/tadpole/deps/SDL2/SDL2Targets.cmake
Call Stack (most recent call first):
  deps/SDL_ttf/CMakeLists.txt:47 (find_package)

The error happens when those subdirs try to find_package(SDL2 REQUIRED), but I don’t understand enough about how the projects are set up to know how to debug this further.

Has anyone here done something like this before? What more do I need to do to get SDL_image and SDL_ttf building?

Might have to jump forward to FetchContent so that SDL & co. are built at configuration time. That way find_package can do its magic.

1 Like

Hey! I am having the same problem and I am already using FetchContent, I can’t seem to find _deps/sdl2_content-src/SDL2Targets.cmake at my build directory. How did you fixed this?

For me the right place to search for the target would be "${sdl2_content_BINARY_DIR}/CMakeFiles/Export/lib/cmake/SDL2/" but the SDL2config.cmake has the following path in it:
include("${CMAKE_CURRENT_LIST_DIR}/SDL2Targets.cmake")

Is there a way to overwrite CMAKE_CURRENT_LIST_DIR and pass the correct PATH?

All things I have tried I endup getting an error

in function "`SDL_floor_REAL'" undefined reference floor

My full file is here: https://stackoverflow.com/questions/64960863/fetch-build-and-link-of-sdl2-fails-with-undefined-floor-references-using-cmak

The errors happen in both Ubuntu 20.04 and Ubuntu 16.04 . Curiously, my Android build with CMake is fine.

Thanks for the suggestion! I ended up just deleting the find_package calls and changed the references from SDL2::SDL2 et al to just SDL2. That seems to make it work for now.

Sorry, I didn’t have that error!

It’s alright, I eventually did like this and it appears to work.

ExternalProject_Add(
    SDL2
    URL https://www.libsdl.org/release/SDL2-2.0.12.tar.gz
    URL_HASH MD5=783b6f2df8ff02b19bb5ce492b99c8ff
    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/SDL2/build/${build_type_dir}
    INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}
    CMAKE_ARGS ${build_type_arg}
               -D CMAKE_INSTALL_PREFIX=<INSTALL_DIR>)