38

I'm looking for the simplest way to compile a c++ program using SDL2 and SDL_image with cmake.

Here is my best attempt, after hours of searching:

CMakeLists.txt

project(shooter-cmake2)

cmake_minimum_required(VERSION 2.8)

set(SOURCES
shooter.cpp
classes.cpp
utils.cpp
)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

add_executable(${PROJECT_NAME} ${SOURCES})

INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2_image REQUIRED sdl2_image)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY})

I get these errors:

In function `loadTexture(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, SDL_Renderer*)':
undefined reference to `IMG_LoadTexture'
collect2: ld returned 1 exit status

Here is the function call:

#include "SDL.h"
#include "SDL_image.h"

SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
    SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
    texture != nullptr or die("LoadTexture");
    return texture;
}
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Carl Lemaire
  • 383
  • 1
  • 3
  • 7
  • 1
    Remember that SDL is a *C* library, and not a C++ library. So using C++ classes like `std::string` as arguments will most likely not work. – Some programmer dude May 24 '14 at 23:10
  • Also, since you're new here on SO, if you have a question regarding build (compiler or linker) errors, please include the *complete* and *unedited* error log in your question, including the code that causes the error (preferably a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)). In your case please **edit your question** to include the code where you call `IMG_LoadTexture` together with enough context to understand the code (like variable declarations etc.). – Some programmer dude May 24 '14 at 23:11
  • 1
    Don't know any cmake specifics, but shouldn't you add `${SDL2_IMAGE_LIBRARIES}` as well? – keltar May 25 '14 at 07:29
  • @keltar I tried: same errors. (I edited my post) – Carl Lemaire May 25 '14 at 16:46

6 Answers6

57

I think that the following will work, as it finds the libraries on my ubuntu system and the example function you provided can link:

project(shooter-cmake2)

cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

add_executable(${PROJECT_NAME} src/test.cpp)

INCLUDE(FindPkgConfig)

PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)

INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES})

If cmake is executed with --debug-output it outputs:

-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26") 
Called from: [2]    /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake
            [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
-- checking for one of the modules 'sdl2'
Called from: [1]    $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
-- checking for one of the modules 'SDL2_image>=2.0.0'
Called from: [1]    $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt

This made me check the contents of

/usr/lib/x86_64-linux-gnu/pkgconfig/sdl2.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/SDL2_image.pc

I noticed that SDL2_image.pc contains Name: SDL2_image which I assumed should match the third parameter to PKG_SEARCH_MODULE for this library.

wojciii
  • 4,253
  • 1
  • 30
  • 39
  • Thanks, that worked like a charm! Where did you find how to do this? What reference? – Carl Lemaire May 26 '14 at 03:16
  • I did not use any reference just what I could read from the source of the module you use and what I know about how .pc files work .. I updated the answer with additional info. :) – wojciii May 26 '14 at 09:14
  • Thanks for the edit! But how has `--debug-output` helped you? I'm just trying to understand how you worked it out. – Carl Lemaire May 26 '14 at 22:01
  • The debug output told me that pkg was used and that one library configured used one pkg file was working while another was not. That narrows it down. – wojciii May 27 '14 at 08:24
  • 3
    Since CMake 3.1, `set(CMAKE_CXX_STANDARD yy)` is preferred over modifying `CMAKE_CXX_FLAGS`. – Emil Laine Jan 24 '16 at 17:04
  • You should definitely improve the answer. ;) – wojciii Jan 25 '16 at 11:09
  • That was awesome! Thanks! – sinner Jul 18 '16 at 00:32
  • In my case the modules name has to be `SDL2_IMAGE` instead of `SDL2IMAGE`. – JusticeJuice Dec 08 '16 at 21:16
  • Woah, so current Ubuntu 18.04 fails to locate SDL2_image with `find_package`, but *this* works!? I'm really trying (this time) not to use explicit paths, and found that on some systems I have to use `find_package` in "config" mode - but now this? I read then that `FindPkgConfig` is somehow "external" to CMake...but if it does find the elusive SDL2_image, maybe it will find all my other libraries too...is this the future or the past...one ring to rule them all...it'll only take a few more hours to check... – user2023370 Aug 24 '18 at 15:11
11

There are two blog posts about this here:

Using SDL2 with CMake

Using SDL2_image with CMake

Basically you need a FindSDL2.cmake and FindSDL2_image.cmake module. They can be based of the ones that work for SDL 1.2 which are included in CMake already. Using these Find modules will also work on Windows.

If you are on Linux and only need SDL2 you don't even need the FindSDL2.cmake as the following already works:

cmake_minimum_required(VERSION 3.7)

project(SDL2Test)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable(SDL2Test Main.cpp)
target_link_libraries(SDL2Test ${SDL2_LIBRARIES})
trenki
  • 7,133
  • 7
  • 49
  • 61
6

I was having trouble with these answers, I think cmake changed the way to import targets. Following @trenki blog post I needed to change my CMakeLists.txt to:

project(SDL2Test)
find_package(SDL2 REQUIRED COMPONENTS SDL2::SDL2)
add_executable(SDL2Test main.cpp)
target_link_libraries(SDL2Test SDL2::SDL2)

Currently this works out of the box on Arch Linux.

Charles
  • 125
  • 1
  • 6
  • I also saw some similar double colon stuff, but couldn't get it to work with SDL2_image. It all depends on whether different flavours of ad-hoc "find" configuration files are present on your system. CMake does demand your patience. Can we not all just agree to put our files in the same place :) – user2023370 Aug 24 '18 at 15:17
5

I introduced a modern and portable approach for linking to the SDL2, SDL2_image. These SDL2 CMake modules let you build an SDL2 & SDL2_image project as follows :

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image)

You should just clone the repo in your project:

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

Note: If CMake didn't find the SDL2/SDL2_image libraries (in Windows), we can specify the CMake options SDL2_PATH and SDL2_IMAGE_PATH as follows:

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

It supports also other related libraries : SDL2_ttf, SDL2_net, SDL2_mixer and SDL2_gfx. For more details, please read the README.md file.

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

aminosbh
  • 193
  • 1
  • 5
3

Since 2.6 version, SDL2_image installation is shipped with CMake config script SDL2_imageConfig.cmake/SDL2_image-config.cmake.

So find_package(SDL2_image) works without any additional FindSDL2_image.cmake module, and creates IMPORTED target SDL2_image::SDL2_image:

find_package(SDL2_image REQUIRED)
target_link_libraries(<executable-target> SDL2_image::SDL2_image)

Note, that variables like SDL2_IMAGE_LIBRARIES or SDL2_IMAGE_INCLUDE_DIRS are NOT set in this case, so using them is meaningless.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
0

The following commands works fine for me:

set(SDL_INCLUDE_DIR "/usr/include/SDL2")
set(SDL_LIBRARY "SDL2")
include(FindSDL)  

if(SDL_FOUND)  
  message(STATUS "SDL FOUND")
endif()