cmacoscmakesdl-2sdl-ttf

How to use SDL2_ttf in a CMake project?


I have my project in C using SDL2 and SDL2_ttf.

Here is my cmakelists file:

cmake_minimum_required(VERSION 3.5.0)
project(mygame VERSION 0.1.0 LANGUAGES C)

find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2TTF_INCLUDE_DIR})

add_executable(mygame main.c
    classes/list/list.c
    classes/my_string/my_string.c
    classes/errors/errors.c
    classes/render/render.c
    classes/constants/constants.c
)
target_link_libraries(mygame PRIVATE ${SDL2_LIBRARIES} ${SDL2TTF_LIBRARY})

So after compilation I have SDL2/SDL_ttf.h not found error:

[main] Building folder: /Users/ilababakov/Documents/code/Equation_solver/build all
[build] Starting build
[proc] Executing command: /opt/homebrew/bin/cmake --build /Users/ilababakov/Documents/code/Equation_solver/build --config Debug --target all -j 10 --
[build] [ 14%] Building C object CMakeFiles/mygame.dir/main.c.o
[build] [ 42%] Building C object CMakeFiles/mygame.dir/classes/list/list.c.o
[build] [ 42%] Building C object CMakeFiles/mygame.dir/classes/errors/errors.c.o
[build] [ 71%] Building C object CMakeFiles/mygame.dir/classes/render/render.c.o
[build] [ 71%] Building C object CMakeFiles/mygame.dir/classes/my_string/my_string.c.o
[build] [ 85%] Building C object CMakeFiles/mygame.dir/classes/constants/constants.c.o
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/render/render.c:1:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/render/render.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build]          ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] make[2]: *** [CMakeFiles/mygame.dir/classes/render/render.c.o] Error 1
[build] make[2]: *** Waiting for unfinished jobs....
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/constants/constants.c:1:
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/constants/constants.h:6:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/constants/../errors/errors.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build]          ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] make[2]: *** [CMakeFiles/mygame.dir/classes/constants/constants.c.o] Error 1
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/errors/errors.c:1:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/errors/errors.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build]          ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/main.c:2:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/render/render.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build]          ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] make[2]: *** [CMakeFiles/mygame.dir/classes/errors/errors.c.o] Error 1
[build] make[2]: *** [CMakeFiles/mygame.dir/main.c.o] Error 1
[build] make[1]: *** [CMakeFiles/mygame.dir/all] Error 2
[build] make: *** [all] Error 2
[proc] The command: /opt/homebrew/bin/cmake --build /Users/ilababakov/Documents/code/Equation_solver/build --config Debug --target all -j 10 -- exited with code: 2
[driver] Build completed: 00:00:00.161
[build] Build finished with exit code 2

I've installed both SDL2 and ttf via brew and they are both found (a least cmake doesn't complain when i'm trying to find_package), but error is still there. Compiling project without SDL2_ttf is all good. I'm using vscode cmake extention on macos sonoma 14.2.1.


Solution

  • Calling find_package(SDL2_ttf) creates IMPORTED target SDL2_ttf::SDL2_ttf. Just link your target with it, and your target will obtain all needed include directories and libraries:

    target_link_libraries(mygame PRIVATE SDL2_ttf::SDL2_ttf)