如何解决SDL中未找到IMG_Load引用错误?

问题描述

我正在尝试使用Snake game使用SDL构建一个简单的应用程序。我想使用SDL_Image在渲染的窗口中显示一个简单的图像,但是编译失败并出现错误。 游戏运行正常,但是当我尝试添加代码以加载png图像并使用默认cmake进行编译时,它失败了。

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'

我尝试更改SDL和SDL_image的版本,但均无济于事。我检查了libsdl文件中的符号,好像已定义了IMG_Load。所有文件都保留在蛇游戏链接中。

编辑: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})

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)