CLIon - 使用SDL_TTF的未定义引用

时间:2017-10-25 22:28:43

标签: c++ makefile sdl clion sdl-ttf

我正在编写一个小游戏,我正在使用SDLs库作为图形(使用CLion IDE)。我已经下载了SDL2,SDL2_image和SDL2_ttf。在代码中,我包含了三个库并使用TTF来制作一些文本:

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

bool foo() {

    if(SDL_Init(SDL_INIT_EVERYTHING)) {
        return false;
    }

    if (TTF_Init() == -1){
        cerr << "Error ." << endl;
    }

    TTF_Font* font = TTF_OpenFont("Sans.ttf", 20);
    SDL_Color color = {100, 0, 0};
    SDL_Surface* text;

    ...

另外,我有以下makefile链接这些库:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

set(CLIENT_FILES core/client.h core/client.cpp)
set(CONFIGURATION_FILES configuration/configurationClient.cpp configuration/configurationClient.h)
file(GLOB_RECURSE GAME_FILES "game/*.cpp" "game/*.h")
file(GLOB_RECURSE MENU_FILES "menu/*.cpp" "menu/*.h")

add_executable(client main.cpp ${CLIENT_FILES} ${MENU_FILES} ${CONFIGURATION_FILES} ${GAME_FILES})

include(FindPkgConfig)

PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
PKG_SEARCH_MODULE(SDL2TTF REQUIRED SDL2_ttf>=2.0.0)

include_directories(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${SDL2TTF_INCLUDE_DIRS})

target_link_libraries(client common SDLPrimitives ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES} ${SDL2TTF_LIBRARIES})

我的问题是IDE识别SDL_ttf库(它没有用红色标记为TTF函数的错误),但是当我尝试编译代码时,我得到了许多未定义的引用。

CMakeFiles/client.dir/menu/menuClientVisual.cpp.o: In function `foo()':
source/client/menu/foo.cpp:137: undefined reference to `TTF_Init'
source/client/menu/foo.cpp:141: undefined reference to `TTF_OpenFont'
source/client/menu/foo.cpp:145: undefined reference to `TTF_RenderText_Solid'
source/client/menu/foo.cpp:152: undefined reference to `TTF_RenderText_Solid'
source/client/menu/foo.cpp:157: undefined reference to `TTF_SetFontStyle'
source/client/menu/foo.cpp:160: undefined reference to `TTF_RenderText_Solid'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试包括:

find_library(SDL2TTF_LIBRARIES SDL_ttf)