Using CMake with SDL2

Out of the box, CMake isn’t able to find the SDL2 library. You’ll get error messages such as:

CMake Error at CMakeLists.txt:8 (find_package):
  By not providing "FindSDL2.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SDL2", but
  CMake did not find one.

  Could not find a package configuration file provided by "SDL2" with any of
  the following names:

    SDL2Config.cmake
    sdl2-config.cmake

  Add the installation prefix of "SDL2" to CMAKE_PREFIX_PATH or set
  "SDL2_DIR" to a directory containing one of the above files. If "SDL2"
  provides a separate development package or SDK, be sure it has been
  installed.

Being rather new to CMake (and not knowing how to create a decent FindSDL2.cmake module), I initially solved the problem by referencing the files manually:

set(SDL2_INCLUDE_DIR /usr/include/SDL2)
set(SDL2_LIBRARY /usr/lib/libSDL2.so)

However a much better way is to use the FindSDL2.cmake module that’s been floating around the internet. Simply download, place it in a dedicated folder, and add that folder to the CMAKE_MODULE_PATH.

Here is a complete CMakeLists.txt file that can compile a simple SDL2 App

cmake_minimum_required(VERSION 2.8)
project(SDL2_App)

# includes cmake/FindSDL2.cmake
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})

set(SOURCE_FILES src/main.cpp)

add_executable(SDL2_App ${SOURCE_FILES})
target_link_libraries(SDL2_App ${SDL2_LIBRARY})

Advertisements

4 thoughts on “Using CMake with SDL2

  1. Hi Brendan,

    I am doing like you said and followed the link to use FindSDL2.cmake module but still im getting this error: could not find SDL2 (missing: SDL2_LIBRARY) in CLion 1.1

    I have in my project cmake folder which includes FINDSDL2.cmake, FINDSDL2_image.cmake, FINDSDL2_tff.cmake and in the root project folder i have CMakeLists.txt that CLion uses.

    In CMakeLists.txt i have the following :
    ——————————————————————————
    cmake_minimum_required(VERSION 3.3)
    project(SDL_test)

    # includes cmake/FindSDL2.cmake
    set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

    find_package(SDL2 REQUIRED)
    include_directories(${SDL2_INCLUDE_DIR})

    set(SOURCE_FILES src/main.cpp)

    add_executable(SDL2_App ${SOURCE_FILES})
    target_link_libraries(SDL2_App ${SDL2_LIBRARY})
    ——————————————————————————

    My SDL2 folder is in the root of my harddrive c:\SDL2-2.0.3

    But i have also tried to have folder in my project named SDL2 and it does not work.

    I have tried to follow everything i find on google about using SDL2 with CLion but it seems nothing works. I always get the same error: could not find SDL2 (missing: SDL2_LIBRARY)

    Do you have any ideas that could help me solve this problem, it’s frustrating to be not able to use CLion because i prefer it over Visual Studio.

      • Edit: The version of FindSDL2 linked here doesn’t have SDL2_PATH. Add it by adding a line ${SDL2_PATH} to SET(SDL2_SEARCH_PATHS) in FindSDL2.cmake

    • This is the second result on google if you search for “sdl2 cmake”
      This line:
      target_link_libraries(SDL2_App ${SDL2_LIBRARY})
      Should now be:
      target_link_libraries(SDL2_App ${SDL2_LIBRARIES})
      Thanks for the quick reference.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s