0

I've been trying to setup the extension library SDL2_image for SDL2 on CLion (MingW). Steps for installation I've taken so far:

  1. Copy the contents of SDL2_image development into C:\MingW
  2. Created a FindSDL2_IMAGE.cmake file and linked it.

Here's my CMakeList.txt file:

cmake_minimum_required(VERSION 3.19)
project(TestGame)

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

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

find_package(SDL2_IMAGE REQUIRED)
include_directories(${SDL2_IMAGE_INCLUDE_DIR})

add_executable(TestGame src/main.cpp src/GameWindow.cpp src/GameWindow.h)

target_link_libraries(TestGame ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})

And the relevant main.cpp file:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
int main(int argc, char *args[]) {
    IMG_Init(IMG_INIT_PNG);
}

Edit: I've forgot to include the error message.

CMakeFiles\TestGame.dir/objects.a(main.cpp.obj): In function `Z4initv':
D:/_dev/C++/TestGame/src/main.cpp:14: undefined reference to `IMG_Init'
genpfault
  • 51,148
  • 11
  • 85
  • 139
Magikarpm
  • 1
  • 3
  • What is content of `FindSDL2_IMAGE.cmake` file which you create? Exactly that file is responsible for filling `SDL2_IMAGE_LIBRARIES` variable which you use for link with. – Tsyvarev Oct 04 '21 at 19:28
  • @Tsyvarev I've used the cmake file mentioned in [this](https://stackoverflow.com/a/31831152/13993836) answer. – Magikarpm Oct 04 '21 at 19:48
  • The script you refers to sets `SDL2_IMAGE_LIBRARY` variable, not `SDL2_IMAGE_LIBRARIES` one which you are using. – Tsyvarev Oct 04 '21 at 20:03
  • @Tsyvarev with all due respect, could you elaborate on what the problem is? – Magikarpm Oct 04 '21 at 20:16
  • 1
    In the referenced answer they use `${SDL2_IMAGE_LIBRARY}` construction in `target_link_libraries` call. That construction is a dereference of the variable `SDL2_IMAGE_LIBRARY`, which is **actually** set by their `FindSDL2_image.cmake` script. In your `CMakeLists.txt` the call to `target_link_libraries` uses construction `${SDL2_IMAGE_LIBRARIES}`, which dereferences variable `SDL2_IMAGE_LIBRARIES`. But this variable is **not set** by the script, so you link with nothing. When you want to refer to a variable, you need to properly spell its name. – Tsyvarev Oct 04 '21 at 20:44
  • 1
    @Tsyvarev Wow, thank you so much! Changing `SDL2_IMAGE_LIBRARIES` to `SDL2_IMAGE_LIBRARY` worked. Feel free to submit an answer so I can mark the question as answered. – Magikarpm Oct 05 '21 at 10:31

0 Answers0