对TTF_INIT的未定义引用

Undefined reference to TTF_Init

本文关键字:未定义 引用 INIT TTF      更新时间:2023-10-16

我使用sdl2 lib制作一个简单的游戏。成功测试了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)

main.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
CMakeFilesuntitled17.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
CMakeFilesuntitled17.dirbuild.make:96: recipe for target 'untitled17.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/untitled17.dir/all] Error 2
CMakeFilesMakefile2:66: recipe for target 'CMakeFiles/untitled17.dir/all' failed
CMakeFilesMakefile2: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)