Ovenkoek Ovenkoek - 1 year ago 250
C++ Question

Using SDL2 with CMake

I'm trying to use CLion to create a SDL2 project.
The problem is that the SDL headers can't be found when using #include's.

My CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.4)

set(SDL2_INCLUDE_DIR C:/SDL/SDL2-2.0.3/include)
set(SDL2_LIBRARY C:/SDL/SDL2-2.0.3/lib/x64)

set(SOURCE_FILES main.cpp)

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

My test main.cpp:

#include <iostream>
#include "SDL.h" /* This one can't be found */

int main(){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
return 0;

Thank you for any help you could give me.

I'm using Windows and CLion is configured to use cygwin64.

Answer Source

Don't set the path to SDL by hand. Use the proper find command which uses FindSDL. Should look like:

add_executable(ChickenShooter main.cpp)
target_include_directories(ChickenShooter ${SDL2_INCLUDE_DIR})
target_link_libraries(ChickenShooter ${SDL2_LIBRARY})    

If SDL is not found, you have to add the path to SDL to CMAKE_PREFIX_PATH, that's the place where CMake looks for installed software.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download