-2

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})
genpfault
  • 51,148
  • 11
  • 85
  • 139
user3840530
  • 290
  • 1
  • 4
  • 14
  • Youre sure you're properly linking against SDL_image? because every other duplicate I can find revolves around linking issues with that particular library. – Borgleader Sep 16 '20 at 11:45
  • @Borgleader I also found the same. I have seen many posts referring to the linking issues but given the cmake structure in the link, where should I make the changes, I couldn't figure out. I am new to SDL and don't know how I should proceed. Thanks. – user3840530 Sep 16 '20 at 11:49
  • Please, add the code (`CMakeLists.txt`), which reflects your attempt, into the **question post**. This is a requirement of Stack Overflow for a question post to include relevant code in its body. A *link* to the code is insufficient for fulfill this requirement. See also [ask]. – Tsyvarev Sep 16 '20 at 12:53
  • You write that you have tried to link with `SDL_image`, but I see no such attempts in your code. Have you checked [that question](https://stackoverflow.com/questions/23850472/how-to-use-sdl2-and-sdl-image-with-cmake) about linking with `SDL_image` in a CMake project? Have you tried to follow solution suggested in [that answer](https://stackoverflow.com/a/23858115/3440745)? Or [that one](https://stackoverflow.com/a/54772241/3440745)? – Tsyvarev Sep 16 '20 at 14:02
  • @Tsyvarev Thanks for your comment. I am including the results of the suggested link and what I tried. – user3840530 Sep 16 '20 at 15:02

1 Answers1

0

Since I was searching with "IMG_Load reference error" I found answers like "install additional libraries" or "include the flag -lSDL2_image".

After trying to install and revert different sdl versions, I found something that worked for me: The "/usr/lib/x86_64-linux-gnu/cmake/SDL2/sdl2-config.cmake" file has a flag -ISDL. I appended -ISDL_image to it as suggested in link and the linking errors were gone. I am not sure if this is even close to something that one should do.

I also tried the link suggested by @Tsyvarev and included pkg_config changes to search for SDL_image. It failed saying:

None of the required 'SDL2_image>=2.0.0' found

I then used suggestion from this post and installed the sdl_image library which removed the errors.

Thanks for the suggestions!

user3840530
  • 290
  • 1
  • 4
  • 14