I am trying to use the Snake game for building a simple application using SDL. I want to display a simple image in the rendered window using SDL_Image but the compilation fails with an error. The game works fine but the moment I try to add code to load a png image and compile using the default cmake, it fails.
renderer.cpp:
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
#inside the render function, I added the below lines.
SDL_Surface* surface = IMG_Load("resources/map.png");
SDL_Texture* texture = SDL_CreateTextureFromSurface(sdl_renderer, surface);
SDL_Rect map_background;
map_background.x = 0;
map_background.y = top_bar_width;
map_background.w = screen_width;
map_background.h = screen_height;
SDL_RenderCopy(sdl_renderer, texture, NULL, &map_background);
**ERROR:**
renderer.cpp:(.text+0x265): undefined reference to `IMG_Load'
I tried changing verions of both SDL and SDL_image but to not avail. I checked for symbols in the libsdl files and seems like IMG_Load is defined. All files are kept as it is in the snake game link.
Edit: CmakeLists.txt:
cmake_minimum_required(VERSION 3.7)
add_definitions(-std=c++17)
set(CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS, "${CXX_FLAGS}")
project(SDL2Test)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} src)
add_executable(SnakeGame src/main.cpp src/game.cpp src/controller.cpp src/renderer.cpp src/snake.cpp)
string(STRIP ${SDL2_LIBRARIES} SDL2_LIBRARIES)
target_link_libraries(SnakeGame ${SDL2_LIBRARIES})