c++cmakesdlclionsdl-ttf

Problem with using SDL_ttf in CLion on Windows (C++)


I am trying to use SDL_ttf in CLion on Windows to display text on screen within a game loop. I don't have any issues with standard SDL, which I installed in C:/Programs/SDL/SDL2-2.30.2 (original download file was SDL2-devel-2.30.6-mingw.zip) I also downloaded SDL-TTF into the same location: C:/Programs/SDL/SDL2_ttf-2.22.0 (original download file was SDL2_ttf-devel-2.22.0-mingw.zip) My CMakeLists.txt file contains the following:

cmake_minimum_required(VERSION 3.28)
project(MyProject)

set(CMAKE_CXX_STANDARD 17)

find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)

message(STATUS "SDL2_LIBRARIES = " ${SDL2_LIBRARIES})  # SDL2::SDL2mainSDL2::SDL2

add_executable(MyProject main.cpp
        ...)


include_directories("C:/Program Files/SDL/SDL2_ttf-2.22.0/x86_64-w64-mingw32/include/SDL2")
target_link_libraries(MyProject "C:/Program Files/SDL/SDL2_ttf-2.22.0/x86_64-w64-mingw32/lib/libSDL2_ttf.a")

target_link_libraries(MyProject ${SDL2_LIBRARIES} ${SDL2_TTF_LIBRARIES})

This builds without any errors. I can also include #include "SDL_ttf.h" in my main.cpp file without getting errors. The problem occurs, when I try to actually load a font within my game loop:

...
while (true) {
  TTF_Font* font = TTF_OpenFont("C:/Windows/Fonts/arial.ttf", 24);  // Leads to error
}

The error is the following:

C:\Program Files\JetBrains\CLion 2024.1\bin\mingw\bin/ld.exe: C:/Program Files/SDL/SDL2_ttf-2.22.0/x86_64-w64-mingw32/lib/libSDL2_ttf.a(libSDL2_ttf_la-hb-uniscribe.o): in function `_hb_generate_unique_face_name':
/Users/valve/release/SDL2_ttf/SDL2_ttf-2.22.0-source/foo-x64/../external/harfbuzz/src/hb-uniscribe.cc:300: undefined reference to `__imp_UuidCreate'

Did I set my CMake file up incorrectly? Are there other issues within my approach? Am I missing any downloads?

Thank you in advance!

When I just define the command TTF_Font* font there seems to be no problem and the game runs fine. So the problem seems to be with the TTF_OpenFont(...) function.

I also tried to change the CMake file to use .dll files:

include_directories("C:/Program Files/SDL/SDL2_ttf-2.0.12/include")
target_link_libraries(MyProject "C:/Program Files/SDL/SDL2_ttf-2.0.12/lib/x86/SDL2_ttf.dll")

This changes the error to undefined reference to TTF_OpenFont'`


Solution

  • The release you downloaded ships with a CMake config file, which means that find_package(SDL2_ttf REQUIRED) defines a target called SDL2_ttf::SDL2_ttf that you can just link.

    Crucially, this target sets up the include directories, location of the file to link, and some extra dependency libraries that you forgot:

    set(_sdl2ttf_extra_static_libraries " -lusp10 -lgdi32 -lrpcrt4  -lusp10 -lgdi32 -lrpcrt4")
    

    That means you do not have to call include_directories for SDL2_ttf or link directly to the DLL, you can just do:

    target_link_libraries(MyProject SDL2_ttf::SDL2_ttf SDL2::SDL2)
    

    as SDL2 exports a similar CMake target.