【问题标题】:cmake cannot find libraries installed with vcpkgcmake 找不到使用 vcpkg 安装的库
【发布时间】:2019-08-25 01:38:44
【问题描述】:

我想在 Windows 的 CMake 项目中使用 vcpkg,因为我需要由这个包管理器处理的 boostxerces

我有以下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)

Boostxerces-cvcpkg 一起安装。由于我使用的是 Visual Studio Code,因此我在 settings.json 中设置了 vcpkg 变量:

  "cmake.configureSettings": {
    "CMAKE_TOOLCHAIN_FILE" : "some/path/vcpkg/scripts/buildsystems/vcpkg.cmake",
    "VCPKG_TARGET_TRIPLET": "x64-windows"
  }

当我运行 che 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 将下载并构建所需的构建包。

我也试过命令行:

 cmake -DCMAKE_TOOLCHAIN_FILE=D:/Projects/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows ../

但结果是一样的。

我做错了什么?怎样才能成功使用vcpkg?

【问题讨论】:

    标签: c++ boost cmake xerces vcpkg


    【解决方案1】:
    1. 您需要预先安装软件包(使用 vcpkg install )。

    (然后您可以将工具链指定为 CMake 选项:

    -DCMAKE_TOOLCHAIN_FILE=C:\path\to\vcpkg\scripts\buildsystems\vcpkg.cmake
    

    但是如果你已经指定了一个工具链,这将不起作用,例如在交叉编译时。)

    1. 改为“包含”它以避免此问题:

    在 find_package() 之前将这一行添加到项目 CMakeLists.txt 中:

    include(/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake)
    

    【讨论】:

    • 这样做会设置各种CMAKE_(SYSTEM_)?(PREFIX|LIBRARY|INCLUDE|FRAMEWORK)_PATH cmake 变量以启用 find_*() cmake 函数,但请注意这些路径包括 VCPKG_TARGET_TRIPLET。
    【解决方案2】:

    boost 目前还没有安装,但我期待在执行 cmake 命令期间,vcpkg 会下载并构建所需的构建包。

    据我所知,情况并非如此。您需要预先为您计划使用的三元组(即x64-windows)安装所需的包和vcpkg。然后,您需要确保在运行 CMake 时使用了正确的三元组(检查 CMakeCache.txt 中的 VCPKG_TARGET_TRIPLET 变量)。如果不正确,您可以更改它并使用 CMake 重新配置。

    此外,根据您得到的错误输出,似乎没有使用vcpkg 正确安装xerces。您可以通过运行检查vcpkg 安装的内容:

    vcpkg list --triplet x64-windows

    【讨论】:

    • 我已经执行了命令并且 xerces 似乎已安装:xerces-c:x64-windows 3.2.2-8 Xerces-C++ is a XML parser, for parsing, generat…。在CMakeCache.txt 中,我有正确的三元组:VCPKG_TARGET_TRIPLET:STRING=x64-windows
    • @Jepessen 好的,提升一下怎么样?
    • 对于 XercesC,请从您的 find_package 电话中删除 CONFIG。如果您使用vcpkg 安装它,CMake 应该能够找到该库。
    • 我已经删除了CONFIG,即使它是由 xerces-c vcpkg 包建议的,但没有任何变化。
    • 删除 CMake 缓存中的所有 XercesC 变量并重新配置?
    【解决方案3】:

    理论上它很简单(假设 vcpkg 安装在C:/vcpkg 中,因为它用于 github 操作);

    1. 使用vcpkg install foo 安装“foo”包
    2. 确保您的 CMakeLists.txt 找到并使用该包;
    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})                                     
    
    1. 使用-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake 运行 cmake

    但是...直到我将 --triplet 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:/.... 调用cmake 默认为x64-windows,因此找不到该包。

    当前 vcpkg 默认为较旧的 x86 目标,但现代 Visual Studio(由 githup 操作使用)默认为 x64。解决方法是使用vcpkg -triplet x64-windows install <foo> 安装软件包。我花了很长时间才发现这一点。

    【讨论】:

    • 这是我的问题的解决方案。唯一要提的是,它是--triplet x64-windows,而不是--triple x64-windows
    【解决方案4】:

    我遇到了同样的问题,只是通过以下方式解决了它:

    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

    【讨论】: