0

I use sdl2 lib to make a simple game. Successfully tested sdl windows and keyboard handling. The problem is I can't use TTF to put some text in the window, TTF_Init is not found (Undefined reference to TTF_Init())

CMake file:

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 build output:

[ 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
Andrii Tytarenko
  • 121
  • 1
  • 10

1 Answers1

1

You need to link against the SDL_ttf library. (Needs to be added to target_link_libraries)

target_link_libraries(untitled17 mingw32 SDL2main SDL2 SDL_ttf)
lostbard
  • 5,065
  • 1
  • 15
  • 17