Wednesday, February 7, 2024

[SOLVED] CMake find_package SDL2_ttf fails on MSYS2 + MinGW-w64

Issue

I am trying to build a SDL2 project on Windows under MinGW. The build goes smooth on Arch Linux but when I try to build on Windows I get this error:

CMake Error at CMakeLists.txt:14 (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.

I have installed both mingw-w64-x86_64-SDL2 and mingw-w64-x86_64-SDL2_ttf packages. The CMake configuration seems to be also installed properly (in /mingw64/lib/cmake/SDL2_ttf/sdl2_ttf-config.cmake)

I use a simple CMake 3.16 configuration featuring find_package:

cmake_minimum_required(VERSION 3.16)
project(project VERSION 0.0.1)

file(GLOB SOURCE_FILES
    src/[a-z]*.c
)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED YES)
set(CMAKE_C_EXTENSIONS OFF)

find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)

set(C_FLAGS "-rdynamic")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_FLAGS}")

ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
                      SDL2::SDL2
                      SDL2_ttf
)

Solution

I have been runing the MSYS2 base environment by executing the msys2.exe. That's why cmake was looking for library files inside the /usr/lib/ directory.

@HolyBlackCat have mentioned this post which mentions how to run the MSYS2 terminal with a correct environent. In my case this is mingw64.exe



Answered By - siery
Answer Checked By - David Goodson (WPSolving Volunteer)