我正在尝试遵循有关使用SDL2制作游戏的youtube教程。 对于此游戏,我必须使用SDL2,SDL2_image和SDL2_ttf。当我尝试编译第一个测试时,得到“对函数IMG_load()的未定义引用”。 Google告诉我这是因为CMake中的链接未正确进行,但是从我的判断来看,它确实正确。 cmake和make的输出如下:
tim@Tim-HP:~/Documents/projects/game/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found SDL2: /usr/lib/x86_64-linux-gnu/libSDL2main.a;/usr/lib/x86_64-linux-gnu/libSDL2.so;-lpthread
-- Found SDL2_image: /usr/lib/x86_64-linux-gnu/libSDL2_image.so (found version "2.0.1")
-- Found SDL2_ttf: /usr/lib/x86_64-linux-gnu/libSDL2_ttf.so (found version "2.0.14")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tim/Documents/projects/game/build
tim@Tim-HP:~/Documents/projects/game/build$ make
Scanning dependencies of target SDL_game
[ 50%] Building C object CMakeFiles/SDL_game.dir/main.c.o
/home/tim/Documents/projects/game/main.c: In function ‘main’:
/home/tim/Documents/projects/game/main.c:128:17: warning: implicit declaration of function ‘IMG_load’ [-Wimplicit-function-declaration]
starSurface = IMG_load("figs/angry-shroom.png");
^
/home/tim/Documents/projects/game/main.c:128:15: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
starSurface = IMG_load("figs/angry-shroom.png");
^
[100%] Linking C executable ../SDL_game
CMakeFiles/SDL_game.dir/main.c.o: In function `main':
main.c:(.text+0x2cd): undefined reference to `IMG_load'
collect2: error: ld returned 1 exit status
CMakeFiles/SDL_game.dir/build.make:98: recipe for target '../SDL_game' failed
make[2]: *** [../SDL_game] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/SDL_game.dir/all' failed
make[1]: *** [CMakeFiles/SDL_game.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
在main.c文件中,包含以下标头:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
CMakeLists非常标准:
cmake_minimum_required(VERSION 3.5)
#set binary path. this case 1 above root (build)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/..")
project (SDL_game)
# adds an additional subdirectory to the CMAKE_MODULE_PATH,
# where FindSDL2_image.cmake is contained, which is custom made and allows find_package(SDL2_image REQUIRED) to work
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${SDL_game_SOURCE_DIR}/cmake")
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "/usr/include/SDL2")
add_executable(${PROJECT_NAME} main.c)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES})
我在哪里使用了经常在线使用的FindSDL2_image等。
有人建议我可能做错了吗?