【问题标题】:CLIon - Undefined reference using SDL_TTFCLIon - 使用 SDL_TTF 的未定义引用
【发布时间】:2018-04-07 04:02:34
【问题描述】:

我正在编写一个小游戏,我正在使用 SDL 库来制作图形(使用 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;

    ...

另外,我有以下链接此库的生成文件:

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'

有什么想法吗?

【问题讨论】:

  • 它没有链接到 TTF 库。查看生成的 Makefile 是否尝试链接 TTF 库以及是否设置为正确的路径。

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


【解决方案1】:

尝试包括:

find_library(SDL2TTF_LIBRARIES SDL_ttf)

【讨论】:

  • 我仍然遇到同样的错误。我认为 find_library 和 PKG_SEARCH_MODULO 做的几乎一样,不是吗?
猜你喜欢
  • 2018-09-01
  • 2016-08-21
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多