我想在Windows的CMake项目中使用vcpkg
,因为我需要同时由此程序包管理器处理的boost
和xerces
。
我有以下CMakeLists.txt
:
cmake_minimum_required (VERSION 3.12.0)
project (myproj)
set (CMAKE_PREFIX_PATH ${XERCES_ROOT})
set (Boost_USE_STATIC_LIBS ON)
set (Boost_USE_MULTITHREADED ON)
unset (Boost_INCLUDE_DIR CACHE)
unset (Boost_LIBRARY_DIRS CACHE)
# set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/modules)
find_package (Boost COMPONENTS filesystem regex REQUIRED)
find_package (XercesC CONFIG REQUIRED)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
message (STATUS "binary dir is ${CMAKE_BINARY_DIR}")
include_directories (${CMAKE_BINARY_DIR}/${PROJECT_NAME}/)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..)
include_directories (${Boost_INCLUDE_DIRS})
include_directories (${XercesC_INCLUDE_DIRS})
set (PROJECT_SRC
code.cpp
)
add_library (${PROJECT_NAME} SHARED ${PROJECT_SRC})
add_dependencies (${PROJECT_NAME} UPDATE_RESOURCES)
target_link_libraries (${PROJECT_NAME} ${Boost_LIBRARIES} XercesC::XercesC)
Boost
和xerces-c
与vcpkg
一起安装。由于我使用的是Visual Studio Code,因此我在vcpkg
中设置了settings.json
变量:
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE" : "some/path/vcpkg/scripts/buildsystems/vcpkg.cmake",
"VCPKG_TARGET_TRIPLET": "x64-windows"
}
当我运行CMake时,出现以下错误:
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:2132 (message):
[cmake] Unable to find the requested Boost libraries.
[cmake]
[cmake] Unable to find the Boost header files. Please set BOOST_ROOT to the root
[cmake] directory containing Boost or BOOST_INCLUDEDIR to the directory containing
[cmake] Boost's headers.
[cmake] Call Stack (most recent call first):
[cmake] D:/projects/vcpkg/scripts/buildsystems/vcpkg.cmake:233 (_find_package)
[cmake] src/myroject/CMakeLists.txt:24 (find_package)
[cmake]
[cmake]
[cmake] CMake Error at D:/Projects/vcpkg/installed/x64-windows/share/xercesc/vcpkg-cmake-wrapper.cmake:1 (_find_package):
[cmake] Could not find a package configuration file provided by "XercesC" with any
[cmake] of the following names:
[cmake]
[cmake] XercesCConfig.cmake
[cmake] xercesc-config.cmake
[cmake]
[cmake] Add the installation prefix of "XercesC" to CMAKE_PREFIX_PATH or set
[cmake] "XercesC_DIR" to a directory containing one of the above files. If
[cmake] "XercesC" provides a separate development package or SDK, be sure it has
[cmake] been installed.
[cmake] Call Stack (most recent call first):
[cmake] D:/Projects/vcpkg/scripts/buildsystems/vcpkg.cmake:189 (include)
[cmake] src/ZLA/CMakeLists.txt:25 (find_package)
[cmake]
[cmake]
[cmake] Configuring incomplete, errors occurred!
[cmake] See also "D:/Projects/zla/build/vscode/CMakeFiles/CMakeOutput.log".
[cms-driver] Error during CMake configure: [cmake-server] Configuration failed.
目前,我已经使用vcpkg命令安装了xerces
,虽然目前尚未安装boost,但是我期望在执行cmake命令期间,vcpkg
将下载并构建所需的build包。
我也尝试了命令行:
cmake -DCMAKE_TOOLCHAIN_FILE=D:/Projects/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows ../
但结果是相同的。
我做错了什么?如何成功使用vcpkg?
答案 0 :(得分:1)
boost目前尚未安装,但是我期望在执行cmake命令期间,vcpkg将下载并构建所需的构建软件包。
据我所知并非如此。您需要使用values_list
预先安装想要使用的三元组软件包(即vcpkg
)。然后,您需要确保在运行CMake时使用了正确的三元组(请检查x64-windows
中的VCPKG_TARGET_TRIPLET
变量)。如果不正确,则可以更改它并使用CMake重新配置。
另外,根据收到的错误输出,似乎CMakeCache.txt
也没有使用xerces
正确安装。您可以通过运行以下命令检查vcpkg
安装了什么:
vcpkg
答案 1 :(得分:1)
我遇到了同样的问题,只是用以下方法解决了它:
set(CURL_DIR "C:/Libs/vcpkg/installed/x64-windows/share/curl")
或
list(APPEND CMAKE_PREFIX_PATH "C:/Libs/vcpkg/packages/curl_x64-windows/share/curl/")
我在 C:/ Libs
下安装了vcpkg答案 2 :(得分:1)
(然后,您可以将工具链指定为CMake选项:
-DCMAKE_TOOLCHAIN_FILE=C:\path\to\vcpkg\scripts\buildsystems\vcpkg.cmake
,但是如果您已经指定了工具链(例如在交叉编译时),则此方法将无效。
将此行添加到find_package()之前的项目CMakeLists.txt中:
include(/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake)
答案 3 :(得分:0)
理论上它很简单(假设 vcpkg 安装在 C:/vcpkg
中,因为它用于 github 操作);
vcpkg install foo
安装“foo”包find_package(FOO)
# Use these instead of the package doesn't have proper cmake package support.
# find_path(FOO_INCLUDE_DIRS foo.h)
# find_library(FOO_LIBRARYS foo)
include_directories(${FOO_INCLUDE_DIRS})
target_link_libraries(myprogram ${FOO_LIBRARIES})
-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake
运行 cmake但是...直到我将 --triple x64-windows
添加到 vcpkg install
命令之前,这对我不起作用。
DCMAKE_TOOLCHAIN_FILE
设置各种 CMAKE_(SYSTEM_)?(PREFIX|LIBRARY|INCLUDE|FRAMEWORK)_PATH
变量以启用 find_*() cmake 函数,但请注意这些路径包括 VCPKG_TARGET_TRIPLET。在我的情况下,使用 vcpkg install <foo>
安装的软件包默认为 x86-windows
,但随后使用默认为 -DCMAKE_TOOLCHAIN_FILE=C:/....
的 x64-windows
调用 cmake,因此无法找到该软件包。
当前 vcpkg 默认为较旧的 x86 目标,但现代 Visual Studio(由 gitup 操作使用)默认为 x64。修复方法是使用 vcpkg -triple=x64-windows install <foo>
安装软件包。我花了很长时间走下太多红鲱鱼兔子洞才发现这一点。