c++ - cmake 找不到使用 vcpkg 安装的库

标签 c++ boost cmake xerces vcpkg

我想在 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?

最佳答案

理论上它很简单(假设 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})                                     
    
  3. 运行 cmake

    -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake
    

但是...这对我不起作用,直到我添加 --triplet x64-windowsvcpkg 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> 安装软件包。 。我花了很长时间才发现这一点。

关于c++ - cmake 找不到使用 vcpkg 安装的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55496611/

相关文章:

c++ - SFML 文本未绘制在窗口上

c++ - 强制打印到终端(正在传输 std::cout)

c++ - 在不使用 bitset 的情况下如何知道 C++ 中的位状态?

c++ - std::streampos 是否保证为 unsigned long long?

c++ - 如何在 C++ 中使用 .so 库与 Clion 和 Cmake?

c++ - 试图理解 boost 示例 httpserver3。 shared_ptr 重置方法不清楚

c++ - Boost.Bind 以访问 std::for_each 中的 std::map 元素

c++ - CMake 未将 boost 目录包含在其生成的项目中

cmake - 为什么 CMAKE_LIBRARY_OUTPUT_DIRECTORY 不起作用?

c - 如何使用简单的 CMake 项目正确配置包含路径