Cmake 无法从 vcpkg 找到包配置文件。

huangapple go评论63阅读模式
英文:

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 of 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 the 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)

(Note: I have removed HTML encoding for readability)

英文:

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)

答案1

得分: 1

a) 这是另一个我回答过的堆栈溢出问题的副本,我目前找不到

b) 这是一个常见错误。您的 CMake 生成器是 x64(可能是 Visual Studio 2022,默认为 x64),而您的 VCPKG_TARGET_TRIPLETx86。CMake 不允许混合使用 x64 和 x86。将您的 VCPKG_TARGET_TRIPLET 设为 x64-windows 或将 CMake 生成器切换为 x86

英文:

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

huangapple
  • 本文由 发表于 2023年6月27日 18:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定