PentaKon
PentaKon

Reputation: 4646

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 "SDL.h" which is the correct way.

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.6)
project(sdl2Project)

set(CMAKE_CXX_STANDARD 11)
#This is where sdl2-config.cmake is located
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:/Users/MyUserName/CLibraries/SDL2-2.0.5/x86_64-w64-mingw32/lib/cmake/SDL2")

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

find_package(sdl2 REQUIRED)

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

There is no FindSDL2.cmake file used.

The SDL2 library I downloaded from libsdl.org is located in: C:/Users/MyUserName/CLibraries/SDL2-2.0.5/x86_64-w64-mingw32

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-build-debug
  CMakeLists.txt
  main.cpp

Upvotes: 3

Views: 2719

Answers (1)

rocambille
rocambille

Reputation: 15996

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

Upvotes: 2

Related Questions