2

I get the error:

CMake Error at CMakeLists.txt:11 (find_package):
  By not providing "FindSDL2_ttf.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SDL2_ttf",
  but CMake did not find one.

  Could not find a package configuration file provided by "SDL2_ttf" with any
  of the following names:

    SDL2_ttfConfig.cmake
    sdl2_ttf-config.cmake

  Add the installation prefix of "SDL2_ttf" to CMAKE_PREFIX_PATH or set
  "SDL2_ttf_DIR" to a directory containing one of the above files.  If
  "SDL2_ttf" provides a separate development package or SDK, be sure it has
  been installed.

Here is what my cmake file looks like:

cmake_minimum_required(VERSION 3.14)
project(Smithereens)

set(CMAKE_CXX_STANDARD 17)

add_executable(Smithereens main.cpp)

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

find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_ttf_INCLUDE_DIRS})

target_link_libraries(Smithereens ${SDL2_LIBRARIES} ${SDL2_ttf_LIBRARIES})

Both SDL2 and SDL2_ttf are installed on my machine, I'm sure I'm just linking incorrectly, learning CMake has been a huge headache for me.

  • Do the files its asking for exist on your system? – drescherjm Sep 25 '19 at 23:55
  • ***I'm sure I'm just linking incorrectly*** No that part is correct. find_package is what is failing. – drescherjm Sep 25 '19 at 23:57
  • 2
    `find_package(XXX)` works only if one have `FindXXX.cmake` script or `XXXConfig.cmake` (`xxx-config.cmake`) one. There is `/usr/lib/x86_64-linux-gnu/cmake/SDL2/sdl2-config.cmake` script, so `find_package(SDL2)` works. But for `SDL2_ttf` there is no such script, so you cannot use `find_package()` to find it. Instead, it has `/usr/lib/x86_64-linux-gnu/pkgconfig/SDL2_ttf.pc` script, so it is possible to find it with `pkg-config`. See [that question](https://stackoverflow.com/questions/29191855/what-is-the-proper-way-to-use-pkg-config-from-cmake) about using it in CMake. – Tsyvarev Sep 26 '19 at 00:11

1 Answers1

4

This works pretty well for me, Ubuntu 18.04.

INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
PKG_SEARCH_MODULE(SDL2TTF REQUIRED SDL2_ttf>=2.0.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SOURCE_FILES main.cpp)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

target_include_directories(${PROJECT_NAME} PRIVATE
    include ${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${SDL2TTF_INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} PRIVATE
    ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES} ${SDL2TTF_LIBRARIES})