英文:
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_TRIPLET
是 x86
。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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论