CMake: неопределенная ссылка на img_LOAD, в то время как библиотеки найдены - PullRequest
0 голосов
/ 07 марта 2019

Я пытаюсь следовать руководству YouTube по созданию игры с SDL2.Для этой игры я должен использовать 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 и т. Д., Которые часто используются онлайн.

У кого-нибудь есть предложения, что я могу сделать неправильно?

...