c++ - 对 TTF_Init 的 undefined reference

标签 c++ cmake sdl sdl-ttf

<分区>

我使用 sdl2 库制作了一个简单的游戏。成功测试了 sdl 窗口和键盘处理。问题是我无法使用 TTF 在窗口中放置一些文本,未找到 TTF_Init(未定义对 TTF_Init() 的引用)

CMake 文件:

cmake_minimum_required(VERSION 3.6)
project(untitled17)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")


include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)

set(SOURCE_FILES main.cpp)
add_executable(untitled17 ${SOURCE_FILES})

target_link_libraries(untitled17 mingw32 SDL2main SDL2)

主要.cpp

#include "include/SDL2/SDL.h"
#include "include/SDL2/SDL_ttf.h"  

using namespace std;

int main(int argc, char* argv []) {

SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();
    TTF_Quit();


    return 0;
}

CLion 构建输出:

[ 50%] Building CXX object CMakeFiles/untitled17.dir/main.cpp.obj
[100%] Linking CXX executable untitled17.exe
CMakeFiles\untitled17.dir/objects.a(main.cpp.obj): In function `SDL_main':
C:/Users/1/ClionProjects/untitled17/main.cpp:14: undefined reference to `TTF_Init'
C:/Users/1/ClionProjects/untitled17/main.cpp:15: undefined reference to `TTF_Quit'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [untitled17.exe] Error 1
CMakeFiles\untitled17.dir\build.make:96: recipe for target 'untitled17.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/untitled17.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/untitled17.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/untitled17.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/untitled17.dir/rule] Error 2
Makefile:117: recipe for target 'untitled17' failed
mingw32-make.exe: *** [untitled17] Error 2

最佳答案

您需要链接到 SDL_ttf 库。 (需要添加到target_link_libraries)

target_link_libraries(untitled17 mingw32 SDL2main SDL2 SDL_ttf)

关于c++ - 对 TTF_Init 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44789337/

相关文章:

c++ - ccache 缓存未命中会减慢编译速度

c++ - Cmake:使用导入的对象

qt - 在 Qt Creator 上维护 CMake

安卓工作室 : CMake is not finding Boost header files

c++ - 在 SDL 中移动位图

c++ - 将操作数修改为 cout

c++ - ostringstream 是未定义的类型?

c++ - 防止 winHTTP 写入 STDOUT

c++ - 我对 SDL 的简单介绍程序没有构建。为什么我会收到这些链接器错误?

python - SDL: "working natively in C++"是什么意思?