Konstantine Konstantine - 7 days ago 5
C++ Question

Using SDL2 with CMake in CLion

After hours of scouring the web and SO for a solution I'm at a standstill. Nothing has worked so far for me...

I'm on Windows, using CLion IDE which uses CMake. My goal is to correctly link

SDL2
to my project and use it through
#include "SDL2.h"
which is the correct way.

Now I tried adding a
FindSDL2.cmake
file, tried modifying the
SDL2_SEARCH_PATH
variable, etc. but nothing seems to work. I'm looking for specific instructions for the following:


  • The format of my
    CMakeLists.txt
    file

  • The format of my
    FindSDL2.cmake
    file

  • Specifics regarding the directory where I should have put the
    MingW development library
    of SDL2

  • Any requirements regarding windows ENV variables that I might have to set.



My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.6)
project(sdl2Project)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${sdl2Project_SOURCE_DIR}/cmake")

set(SOURCE_FILES main.cpp)
add_executable(sdl2Project ${SOURCE_FILES})

find_package(SDL2 REQUIRED)

include_directories(${SDL2_INCLUDE_DIR})
target_link_libraries(SDL2 ${SDL2_LIBRARY})


And my
FindSDL2.cmake
file is one I downloaded from this repo:
https://raw.githubusercontent.com/brendan-w/collector/master/cmake/FindSDL2.cmake

The
SDL2
library I downloaded from
libsdl.org
is located in:
C:\Users\MyUserName\CLibraries\SDL2-2.0.5


I have no experience with CMake so I'm unable to truly understand where the problem stems from. What are the steps I need to take in order for it to find the library and link it correctly??

EDIT:

My Project structure is the following:

sdl2Project
cmake
FindSDL2.cmake
cmake-build-debug
CMakeLists.txt
main.cpp

Answer

Looking in your FindSDL2.cmake, you need to provide an hint to CMake about where the library is installed. You could do this by setting an environment variable SDLDIR, but you shouldn't. General advice: you shouldn't use a CMake package that wasn't provided with the sources you're using.

Looking in sources of SDL2, root directory contains a file sdl2-config.cmake.in that should have been configured and installed in your install directory as sdl2-config.cmake: that's the package file you should use.

Am I right guessing the file C:/Users/MyUserName/CLibraries/SDL2-2.0.5/sdl2-config.cmake exists?

If yes, to allow CMake to find it, add your install directory to CMAKE_PREFIX_PATH, before calling find_package:

set(CMAKE_PREFIX_PATH
    ${CMAKE_PREFIX_PATH}
    "C:/Users/MyUserName/CLibraries/SDL2-2.0.5"
)

find_package(sdl2 REQUIRED)

Note the use of "/" in the path instead of "\" which could be interpreted as escaping character. Quotes around the path are only necessary if the path contains whitespaces.

EDIT:

Moreover, you misused target_link_libraries with a wrong target: SDL2 which you don't build in your project, instead of sdl2Project.

You also used a wrong variable: SDL2_LIBRARY instead of SDL2_LIBRARIES; you can see the good variable name by looking in sdl2-config.cmake.

You may consider target_include_directories instead of include_directories, but again the variable name you used is wrong: SDL2_INCLUDE_DIR instead of SDL2_INCLUDE_DIRS.

Try:

target_include_directories(sdl2Project PUBLIC ${SDL2_INCLUDE_DIRS})
target_link_libraries(sdl2Project ${SDL2_LIBRARIES})