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)
project(ChickenShooter)
set(SDL2_INCLUDE_DIR C:/SDL/SDL2-2.0.3/include)
set(SDL2_LIBRARY C:/SDL/SDL2-2.0.3/lib/x64)
include_directories(${SDL2_INCLUDE_DIR})
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;
}
SDL_Quit();
return 0;
}
Thank you for any help you could give me.
Edit: I'm using Windows and CLion is configured to use cygwin64.
I had the same problem and none of the other solutions worked. But I finally got it working by following this solution : How to properly link libraries with cmake?
In a nutshell, the problem was that the SDL2 library was not linked properly in my CMakeLists.txt. And by writing this into the file, it worked (more explainations in the other thread) :
Don't set the path to SDL2 by hand. Use the proper find command which uses FindSDL. Should look like:
If SDL2 is not found, you have to add the path to SDL2 to
CMAKE_PREFIX_PATH
, that's the place where CMake looks for installed software.If you can use Pkg-config, its use might be easier, see How to use SDL2 and SDL_image with cmake
If you feel more comfortable to use a FindSDL2.cmake file similar to FindSDL.cmake provided by CMake, see https://brendanwhitfield.wordpress.com/2015/02/26/using-cmake-with-sdl2/
You don't seems to have a CMake error whike generating your make file. But I think your problem is, the SDL Header are located in a subfolder named "SDL2".
Change your CMakeLists.txt to include
Instead of
You can also pull in the SDL source repository as a submodule and build/link it statically along with your main program via
add_subdirectory()
andtarget_link_libraries()
:(At least as of the
release-2.0.9
tag, possibly earlier.)This blog post shows how you can do it: Using SDL2 with CMake
On Linux you can use a recent CMake (e.g. version 3.7) and using SDL2 works out of the box.
Under Windows you can download the SDL2 development package, extract it somewhere and then create a
sdl-config.cmake
file in the extracted location with the following content:When you now configure inside the CMake-GUI application there will be a
SDL2_DIR
variable. You have to point it to the SDL2 directory where you extracted the dev package and reconfigure then everything should work.You can then include SDL2 headers by just writing
#include "SDL.h"
.