Integrating SDL2 Source into a project with CMake

I decided to write a little program to try integrating the SDL2 source into my project. My intention was to allow this little program to be distributed via source and built on multiple platforms so I wouldn’t need to create separate binaries or have multi-leveled build process. I would like for users to just type “cmake” and it makes the whole project. Naturally I used cmake with my little program with the intent to add the SDL2 source as a subdirectory that is built as part of my cmake build script.

It appeared to work until I tried building my little program in Visual Studio 2013. I got a “Wrong SDL_config.h” error multiple times. What is the trick to building the SDL source and linking it to my project as part of my cmake build script?

Steps to reproduce (in Windows with VS 2013).

  1. Download my example code from github: https://github.com/CptMonkeyFist/TinyTim
  2. In the root folder run configure.bat
  3. In the newly created “build” folder, open the VS2013 solution.
  4. Build the solution.

I figured out my problem. SDL doesn’t want you to specify the include directory for itself. If you do then it uses the default SDL_config.h instead of the platform specific one that it generates for itself when you build it (confusing… I know). All I needed to do was to take my source code and stick it in its own folder named something like “src”. After I do the following:

  1. Create CMakeLists.txt in the (ProjectRoot)/src directory.
  2. Remove the following from (ProjectRoot)/CMakeLists.txt

Code:
include_directories(
${INCLUDE_DIR}/sdl2/include
)

add_executable(TinyTim main.cpp)

and replace it with this:

Code:
add_subdirectory(src)

  1. Add the code removed in step 2 to CMakeLists.txt in the (ProjectRoot)/src directory.
1 Like

Using SDL2 with cmake currently sucks because cmake doesn’t provide a
FindSDL2.cmake and SDL2 doesn’t provide it (or anything else for
integration) either.
So people usually end up copying a FindSDL2.cmake from some other
project (originally based on FindSDL.cmake for SDL1.2 that’s shipped
with cmake), but that’s not really great of course…

https://bugzilla.libsdl.org/show_bug.cgi?id=2464 has a patch that is
supposed to fix that problem, but hasn’t been merged yet.

Cheers,
DanielOn 05/02/2015 05:56 AM, balornt wrote:

I decided to write a little program to try integrating the SDL2 source
into my project. My intention was to allow this little program to be
distributed via source and built on multiple platforms so I wouldn’t
need to create separate binaries or have multi-leveled build process. I
would like for users to just type “cmake” and it makes the whole
project. Naturally I used cmake with my little program with the intent
to add the SDL2 source as a subdirectory that is built as part of my
cmake build script.

It appeared to work until I tried building my little program in Visual
Studio 2013. I got a “Wrong SDL_config.h” error multiple times. What is
the trick to building the SDL source and linking it to my project as
part of my cmake build script?

Steps to reproduce (in Windows with VS 2013).

  1. Download my example code from github:
    https://github.com/CptMonkeyFist/TinyTim
  2. In the root folder run configure.bat
  3. In the newly created “build” folder, open the VS2013 solution.
  4. Build the solution.

+1 from my side, too.Am 08.05.2015 um 21:06 schrieb Daniel Gibson :

Using SDL2 with cmake currently sucks because cmake doesn’t provide a FindSDL2.cmake and SDL2 doesn’t provide it (or anything else for integration) either.
So people usually end up copying a FindSDL2.cmake from some other project (originally based on FindSDL.cmake for SDL1.2 that’s shipped with cmake), but that’s not really great of course…

https://bugzilla.libsdl.org/show_bug.cgi?id=2464 has a patch that is supposed to fix that problem, but hasn’t been merged yet.

Cheers,
Daniel

On 05/02/2015 05:56 AM, balornt wrote:

I decided to write a little program to try integrating the SDL2 source
into my project. My intention was to allow this little program to be
distributed via source and built on multiple platforms so I wouldn’t
need to create separate binaries or have multi-leveled build process. I
would like for users to just type “cmake” and it makes the whole
project. Naturally I used cmake with my little program with the intent
to add the SDL2 source as a subdirectory that is built as part of my
cmake build script.

It appeared to work until I tried building my little program in Visual
Studio 2013. I got a “Wrong SDL_config.h” error multiple times. What is
the trick to building the SDL source and linking it to my project as
part of my cmake build script?

Steps to reproduce (in Windows with VS 2013).

  1. Download my example code from github:
    https://github.com/CptMonkeyFist/TinyTim
  2. In the root folder run configure.bat
  3. In the newly created “build” folder, open the VS2013 solution.
  4. Build the solution.

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks, works for me!

I just encountered this “Wrong SDL_config.h” problem with CMake and figured it out. If you set SDL2 inside a folder and use add_subdirectory to build it and include_directories to include the headers, the order matters. Including SDL2 headers for your project must happen after SDL2 built by itself.

Wrong order:

  # ......
  include_directories(ext/SDL2/include)
  include_directories(include/)
  include_directories(ext/glad/include)
  include_directories(ext/glm/include)

  add_subdirectory(ext/SDL2)
  add_subdirectory(ext/imgui)
  add_subdirectory(ext/glad)
  add_subdirectory(ext/glm)

  file(GLOB SOURCES CONFIGURE_DEPENDS
    src/*.cc
  )

  add_executable(${PROJECT_NAME}
    ${SOURCES}
  )

  target_link_libraries(${PROJECT_NAME}
    glad
    SDL2-static
    glm
    imgui
  )
  # ......

Change the order then it’ll work:

  # ......
  include_directories(include/)
  include_directories(ext/glad/include)
  include_directories(ext/glm/include)

  add_subdirectory(ext/SDL2)
  add_subdirectory(ext/glad)
  add_subdirectory(ext/glm)

  include_directories(ext/SDL2/include)
  add_subdirectory(ext/imgui)

  file(GLOB SOURCES CONFIGURE_DEPENDS
    src/*.cc
  )

  add_executable(${PROJECT_NAME}
    ${SOURCES}
  )

  target_link_libraries(${PROJECT_NAME}
    glad
    SDL2-static
    glm
    imgui
  )
  # ......