Using SDL2 with CMake

sdl cmake visual studio
cmake cross platform example
sdl2 source
sdl gcc
modern cmake sdl2
sdl2 tutorial
sdl h
sdl2 framework download

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.

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

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 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/

Using SDL2 with CMake, 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). Using SDL2 with CMake Linux. Setting up SDL2 with CMake under Ubuntu Linux is pretty easy. All you need to do is install the required Windows. For Windows you have to download the development package SDL2-devel-2.0.5-VC.zip and extract it to some Alternative. An alternative to the config file

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.

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

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:

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 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".

Integrating SDL2 Source into a project with CMake, This project uses SDL2 and OpenGL to render a spinning 3D logo to a desktop window. You can build it on Windows, MacOS or Linux. Requirements. Windows (  SDL2_image is an add-on library for SDL2, the Simple DirectMedia Layer and can be used to load images from various formats. In this post I describe how to get it working with CMake under Windows and Linux. You might also want to check out my other post Using SDL2 with CMake.

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() 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 as of the release-2.0.9 tag, possibly earlier.)

preshing/CMakeDemo: An example cross-platform CMake , Modern CMake modules for finding and using the SDL2 library as well as other related libraries: SDL2_image, SDL2_ttf, SDL2_net, SDL2_mixer and SDL2_gfx. Either we explicitly include and link SDL2 and SDL_gpu by hardcoding the paths to CMakeList or use CMake’s find_package. find_package will search and find the library files. CMake’s find commands work by searching suffixes.

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

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

Instead of

C:/SDL/SDL2-2.0.3/include

aminosbh/sdl2-cmake-modules: Modern CMake modules , Locate SDL library configure –prefix=$SDLDIR used in building SDL. Added code to assist with automated building by using environmental variables and  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.

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

You should just copy the module in cmake/sdl2 (Or just clone the modules repo) in your project:

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

Then add the following lines in 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 didn't find the SDL2 library (in Windows), we can specify the CMake option SDL2_PATH as follows:

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

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

The 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/samples and projects that uses these modules here : https://github.com/aminosbh/sdl-samples-and-projects

FindSDL, cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "SDL2", but CMake did not find  Homepage Using SDL2 with CMake About Since 1985, we have provided essential insights, opportunities and tools to connect the innovative organizations and talented professionals who advance health and quality of life across the globe.

Using CMake with SDL2, To lookup SDL2 we'll need a module to find the dependency we want. Typically these are included in the CMake distribution but for some  If your cmake is new enough and it has FindSDL2.cmake file, you can do this: # Find the SDL2 library find_package(SDL2 REQUIRED) # Add SDL2 into include directories include_directories(${SDL2_INCLUDE_DIR})

Lesson 0: CMake, I am trying to setup SDL using CMake on Ubuntu 18.04, in CLion. I prefer to keep a copy of the source code in my project, rather than what ever my package  Install the SDL2 headers and libraries using MacPorts. sudo port install libsdl2 OpenGL headers and libraries are installed by Xcode. CMake will find them automatically. Ubuntu. Install the SDL2 headers and libraries. sudo apt install libsdl2-dev OpenGL headers and libraries are already present on the system. CMake will find them automatically.

Using SDL with CMake : opengl, Production quality of the video is pretty low. I mostly just wanted to make SOMETHING and put Duration: 4:58 Posted: Jul 3, 2017 If you ever have questions about the meaning or functionality of SDL features head over to the SDL documentation wiki and/or comment on the lesson page. You can also get help with SDL in the SDL Forums or in the IRC at #SDL on freenode. Tutorial Index. Lesson 0: Setting up SDL; Postscript 0: Properly Finding Resource Paths; Lesson 1: Hello World

Comments
  • I've got a Unknown CMake command "find". But I could do it using find_library(SDL) instead.
  • @charlesrockbass: Thanks for the hint, it should be find_package.
  • SDL is not the same as SDL2, and the default FindSDL.cmake does not look for SDL2.
  • github.com/fastogt/fastoplayer/blob/master/src/… crossplatfrom
  • This doesn't work with Mingw on Windows. The library directory and file names are different in the SDL2 Mingw package.