Using SDL2 with CMake

I am trying to use CLion to create an SDL2 project. The problem is that SDL headers cannot be found when using # include.

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; } 

Thanks for any help you could give me.

Edit: I am using Windows, and CLion is configured to use cygwin64.

+16
c ++ cmake sdl-2
source share
10 answers

Do not set the path to SDL2 manually. Use the correct search command that uses FindSDL . It should look like this:

 find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2) find_library(SDL2_LIBRARY NAME SDL2) add_executable(ChickenShooter main.cpp) target_include_directories(ChickenShooter ${SDL2_INCLUDE_DIR}) target_link_libraries(ChickenShooter ${SDL2_LIBRARY}) 

If SDL2 is not found, you must add the path to CMAKE_PREFIX_PATH in CMAKE_PREFIX_PATH , where CMake searches for installed software.

If you can use Pkg-config, using it might be easier, see How to use SDL2 and SDL_image with cmake

If you prefer to use the FindSDL2.cmake file, similar to the FindSDL.cmake file provided by CMake, see https://brendanwhitfield.wordpress.com/2015/02/26/using-cmake-with-sdl2/

+16
source share

This blog post shows how you can do this: Using SDL2 with CMake

On Linux, you can use recent CMake (e.g. version 3.7), and using SDL2 works out of the box.

 cmake_minimum_required(VERSION 3.7) project(SDL2Test) find_package(SDL2 REQUIRED) include_directories(SDL2Test ${SDL2_INCLUDE_DIRS}) add_executable(SDL2Test Main.cpp) target_link_libraries(SDL2Test ${SDL2_LIBRARIES}) 

On Windows, you can download the SDL2 development package, extract it somewhere, and then create the sdl-config.cmake file in the extracted location with the following contents:

 set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include") # Support both 32 and 64 bit builds if (${CMAKE_SIZEOF_VOID_P} MATCHES 8) set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2main.lib") else () set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2main.lib") endif () string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES) 

When you now configure inside the CMake-GUI application, there will be an SDL2_DIR variable. You must point it to the SDL2 directory where you extracted the dev package and reconfigured, then everything should work.

You can then include the SDL2 headers by simply writing #include "SDL.h" .

+10
source share

You do not have a CMake error generating your make file. But I think your problem is that the SDL header is in a subfolder called "SDL2".

Modify your CMakeLists.txt to include

 C:/SDL/SDL2-2.0.3/include/SDL2 

Instead

 C:/SDL/SDL2-2.0.3/include 
+1
source share

Using the SDL2 CMake module that I developed, you can easily integrate the SDL2 library into a modern and portable approach.

You should simply copy the module to cmake/sdl2 (or just clone the cmake/sdl2 modules) in your project:

 git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2 

Then add the following lines to your CMakeLists.txt:

 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2) find_package(SDL2 REQUIRED) target_link_libraries(${PROJECT_NAME} SDL2::Main) 

Note. If CMake did not find the SDL2 library (on Windows), we can specify the CMake parameter SDL2_PATH as follows:

 cmake .. -DSDL2_PATH="/path/to/sdl2" 

For more information, please read the README.md file.

SDL2 CMake modules support other related libraries: SDL2_image, SDL2_ttf, SDL2_mixer, SDL2_net and SDL2_gfx.

You can find a list of examples / examples and projects that use these modules here: https://github.com/aminosbh/sdl-samples-and-projects

+1
source share

at the time of my answer, SDL2 is provided with the sdl2-config executable (as I understand it, the developers call it "experimental"). After the "make install" SDL2, you can try calling it from the terminal using sdl2-config --cflags --libs to find out what it displays.

And then you can add it to your file:

 set(PROJECT_NAME SomeProject) project(${PROJECT_NAME}) execute_process(COMMAND /usr/local/bin/sdl2-config --libs RESULT_VARIABLE CMD_RES OUTPUT_VARIABLE SDL2_CFLAGS_LIBS ERROR_VARIABLE ERR_VAR OUTPUT_STRIP_TRAILING_WHITESPACE) message("SDL2_CFLAGS_LIBS=${SDL2_CFLAGS_LIBS}; CMD_RES=${CMD_RES}; ERR_VAR=${ERR_VAR}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${SDL2_CFLAGS_LIBS}") set(SOURCE_FILES main.cpp) add_executable(${PROJECT_NAME} ${SOURCE_FILES}) 

Here I have a problem - if I put only the executable name without a path, for example

 execute_process(COMMAND sdl2-config --libs <...> 

I get the error "There is no such file", i.e. cmake is not looking in the current path, and I do not know how to write it correctly.

One more notice: in my makefile I am not a user --cflags , because cmake sets are included correctly, and I do not need to explicitly specify them.

0
source share

I had the same problem and none of the other solutions worked. But finally, I earned by following this solution: How to properly link libraries using cmake?

In a nutshell, the problem was that the SDL2 library was not properly linked in my CMakeLists.txt . And writing this to a file, it worked (more explanation in another thread):

 project (MyProgramExecBlaBla) #not sure whether this should be the same name of the executable, but I always see that "convention" cmake_minimum_required(VERSION 2.8) ADD_LIBRARY(LibsModule file1.cpp file2.cpp ) target_link_libraries(LibsModule -lpthread) target_link_libraries(LibsModule liblapack.a) target_link_libraries(LibsModule -L/home/user/libs/somelibpath/) ADD_EXECUTABLE(MyProgramExecBlaBla main.cpp) target_link_libraries(MyProgramExecBlaBla LibsModule) 
0
source share
 cmake_minimum_required(VERSION 2.8.4) project(ChickenShooter) set(SDL2_INCLUDE_DIR C:/SDL/SDL2-2.0.3/include/SDL2) set(SDL2_LIB_DIR C:/SDL/SDL2-2.0.3/lib/x64) include_directories(${SDL2_INCLUDE_DIR}) link_directories(${SDL2_LIB_DIR}) set(SOURCE_FILES main.cpp) add_executable(${PROJECT_NAME} ${SOURCE_FILES}) target_link_libraries(${PROJECT_NAME} SDL2main SDL2) 
0
source share

You can also insert the source SDL repository as a submodule and build / link it statically with the main program using add_subdirectory() and target_link_libraries() :

 cmake_minimum_required( VERSION 3.7.0 ) project( sdl2-demo ) set( SDL_STATIC ON CACHE BOOL "" FORCE ) set( SDL_SHARED OFF CACHE BOOL "" FORCE ) add_subdirectory( external/sdl ) add_executable( sdl2-demo "src/main.cpp" ) target_link_libraries( sdl2-demo SDL2main SDL2-static ) 

(At least from the release-2.0.9 , perhaps earlier.)

0
source share

Let’s highlight the steps, how I could eventually do this using the FindSDL2.cmake module:

  • Download SDL2-devel-2.0.9-VC.zip (or any other version released after this answer was posted) in the "Development Libraries" section of the download page.
  • Extract the zip folder and you should see a folder similar to "SDL2-2.0.9". Paste this folder into the C: \ Program Files (x86) \ folder.
  • Copy the FindSDL2.cmake module and place it in the new "cmake" directory in your project. I found the FindSDL2.cmake file in the response indicated in the “Accepted Answer”: https://brendanwhitfield.wordpress.com/2015/02/26/using-cmake-with-sdl2/
  • Find the line SET(SDL2_SEARCH_PATHS in FindSDL2.cmake and add the copied development directory for SDL2 as a new line: "/Program Files (x86)/SDL2-2.0.9" # Windows
  • In my CMakeLists.txt add this line: set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

After that, starting CMake worked for me. I include the rest of my CMakeLists just in case he additionally clarifies everything that I might have missed:

 cmake_minimum_required(VERSION 2.8.4) project(Test_Project) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # includes cmake/FindSDL2.cmake set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) set(SOURCE_FILES src/main.cpp src/test.cpp) add_executable(test ${SOURCE_FILES}) # The two lines below have been removed to run on my Windows machine #INCLUDE(FindPkgConfig) #PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2) find_package(SDL2 REQUIRED) INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR}) TARGET_LINK_LIBRARIES(chip8 ${SDL2_LIBRARY}) 

Hope this helps someone in the near future.

0
source share

With the compiled version SDL2-2.0.9 with MinGW-w64 on Windows, the following configuration works for me:

 find_package(SDL2 REQUIRED) add_executable(sdl-test ${SOURCES}) target_link_libraries(sdl-test mingw32 SDL2::SDL2main SDL2::SDL2 ) 

Longer explanation

Reading the SDL2Targets.cmake file, I found out that SDL2 provides several goals:

  • SDL2::SDL2main ( lib/libSDL2main.a )
  • SDL2::SDL2 ( lib/libSDL2.dll.a )
  • SDL2::SDL2-static ( lib/libSDL2-static.a )

For each of these, INTERFACE_INCLUDE_DIRECTORIES defined, which means that we do not need to manually include include_directories for SDL2.

But only when adding SDL2::SDL2main and SDL2::SDL2 as target_link_libraries not enough. The g ++ compiler may complain of a "vague reference to" WinMain "."

-lmingw32 compiler options, I found that -lmingw32 libraries -lmingw32 added before -lmingw32 . For the -lmingw32 option -lmingw32 appear in front of the SDL2 libraries, we must also specify mingw32 as the first target_link_libraries . Which will make this configuration work.

The command I used to build it:

 $ mkdir build && cd build && cmake .. -G"MinGW Makefiles" && cmake --build . 

The only minor problem here is the finally generated compiler options, the -lmingw32 option -lmingw32 duplicated. But since this does not affect the layout process, I have ignored it for now.

0
source share

All Articles