Search code examples
cmakevcpkg

Cmake cannot find package config file from vcpkg


I am trying to write CMakeLists.txt for my game but I have a problem using find_package

cmake_minimum_required(VERSION 3.27.0)
project(pong)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(SDL2 REQUIRED)
find_package(SDL2_image CONFIG REQUIRED)
find_package(SDL2_mixer CONFIG REQUIRED)
find_package(SDL2_ttf CONFIG REQUIRED)

target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2main)

file(GLOB SOURCES source/*.cpp)
add_executable(${PROJECT_NAME} ${WIN32_GUI} ${SOURCES})

On the find_package(SDL2 REQUIRED) line, cmake stops and throws an error

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

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

    SDL2Config.cmake
    sdl2-config.cmake

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

I tried adding these lines but it doesn't help

set(CMAKE_TOOLCHAIN_FILE "C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake")
include (C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake
set(VCPKG_TARGET_TRIPLET "x86-windows")

Everything is fine on the vcpkg side. SDL2 installed, vcpkg integrate install used

UPD: I moved set(CMAKE_TOOLCHAIN_FILE "C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake") to the beginning CMakeLists.txt and now it throws this error:

CMake Error at C:/Program Files/vcpkg/scripts/buildsystems/vcpkg.cmake:852 (_find_package):
  Could not find a configuration file for package "SDL2" that is compatible
  with requested version "".

  The following configuration files were considered but not accepted:

    C:/Program Files/vcpkg/installed/x86-windows/share/sdl2/SDL2Config.cmake, version: 2.26.5 (32bit)

Call Stack (most recent call first):
  CMakeLists.txt:18 (find_package)

Solution

  • a) This is a duplicate of another stack overflow question which I answered and cannot find currently

    b) This is a common error. Your CMake Generator is x64 (Probably Visual Studio 2022 which defaults to x64) while your VCPKG_TARGET_TRIPLET is x86. CMake won't allow to mix x64 and x86. Use x64-windows for your VCPKG_TARGET_TRIPLET or switch the CMake Generator to be x86