【问题标题】:CMake does not find Boost on Custom Location on WindowsCMake 在 Windows 上的自定义位置上找不到 Boost
【发布时间】:2020-06-16 08:52:15
【问题描述】:

我已按照Getting Started on Windows 文档中的说明下载并构建了 Boost。 现在我想通过 CMake 将示例程序链接到该版本的 Boost。

这是我当前的文件夹结构:

cmake-boost
    - boost_1_73_0/ (Boost folder)
    - build/
    - CMakeLists.txt
    - cmake-boost.cpp

这是我CMakeLists.txt文件的内容:

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(cmake-boost LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(BOOST_ROOT "boost_1_73_0")
set(BOOST_LIBRARYDIR "boost_1_73_0/stage/lib")

find_package(Boost REQUIRED COMPONENTS regex)

if(Boost_FOUND)
    message(STATUS "Success!")
endif()

add_executable(cmake-boost cmake-boost.cpp)

target_link_libraries(cmake-boost
    PUBLIC
        Boost::regex
)

如果我从build 文件夹内运行cmake ..,我得到的只是以下消息,说它找不到请求的库。

CMake Warning at C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:896 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1018 (_Boost_COMPONENT_DEPENDENCIES)
  C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1694 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:12 (find_package)


CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:2124 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.73.0

  Boost include path:
  C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0


  Could not find the following Boost libraries:

          boost_regex

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.
Call Stack (most recent call first):
  CMakeLists.txt:12 (find_package)


-- Configuring incomplete, errors occurred!

我的 CMakeFile 出了什么问题?

更新:

这是cmake -DBoost_DEBUG=ON ..的输出

-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
-- The CXX compiler identification is MSVC 19.26.28806.0
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1181 ] _boost_TEST_VERSIONS = 1.70.0;1.70;1.69.0;1.69
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1183 ] Boost_USE_MULTITHREADED = TRUE
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1185 ] Boost_USE_STATIC_LIBS =
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1187 ] Boost_USE_STATIC_RUNTIME =
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1189 ] Boost_ADDITIONAL_VERSIONS =
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1191 ] Boost_NO_SYSTEM_PATHS =
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1259 ] Declared as CMake or Environmental Variables:
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1261 ]   BOOST_ROOT = boost_1_73_0
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1263 ]   BOOST_INCLUDEDIR =
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1265 ]   BOOST_LIBRARYDIR = boost_1_73_0/stage/lib
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1267 ] _boost_TEST_VERSIONS = 1.70.0;1.70;1.69.0;1.69
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1342 ] Include debugging info:
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1344 ]   _boost_INCLUDE_SEARCH_DIRS = boost_1_73_0/include;boost_1_73_0;PATHS;C:/local/boost_1_70_0;PATHS;C:/local/boost_1_70;PATHS;C:/local/boost_1_69_0;PATHS;C:/local/boost_1_69;PATHS;C:/boost/include;C:/boost;/sw/local/include
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1346 ]   _boost_PATH_SUFFIXES = boost-1_70_0;boost_1_70_0;boost/boost-1_70_0;boost/boost_1_70_0;boost-1_70;boost_1_70;boost/boost-1_70;boost/boost_1_70;boost-1_69_0;boost_1_69_0;boost/boost-1_69_0;boost/boost_1_69_0;boost-1_69;boost_1_69;boost/boost-1_69;boost/boost_1_69
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1366 ] location of version.hpp: C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/boost/version.hpp
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1391 ] version.hpp reveals boost 1.73.0
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1447 ] Boost_LIB_PREFIX =
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1449 ] Boost_NAMESPACE = boost
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1484 ] guessed _boost_COMPILER = -vc141;-vc140
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1494 ] _boost_MULTITHREADED = -mt
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1578 ] _boost_RELEASE_ABI_TAG = -
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1580 ] _boost_DEBUG_ABI_TAG = -gd
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1643 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = boost_1_73_0/stage/lib;boost_1_73_0/lib;boost_1_73_0/stage/lib;boost_1_73_0/lib64-msvc-14.1;boost_1_73_0/lib64-msvc-14.0;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/lib;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/../lib;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/stage/lib;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/../lib64-msvc-14.1;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/../lib64-msvc-14.0;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/lib64-msvc-14.1;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/lib64-msvc-14.0;C:/local/boost_1_70_0/lib64-msvc-14.1;C:/local/boost_1_70_0/lib64-msvc-14.0;C:/local/boost_1_70/lib64-msvc-14.1;C:/local/boost_1_70/lib64-msvc-14.0;C:/local/boost_1_69_0/lib64-msvc-14.1;C:/local/boost_1_69_0/lib64-msvc-14.0;C:/local/boost_1_69/lib64-msvc-14.1;C:/local/boost_1_69/lib64-msvc-14.0;C:/boost/lib64-msvc-14.1;C:/boost/lib64-msvc-14.0;PATHS;C:/boost/lib;C:/boost;/sw/local/lib_boost_LIBRARY_SEARCH_DIRS_DEBUG   = boost_1_73_0/stage/lib;boost_1_73_0/lib;boost_1_73_0/stage/lib;boost_1_73_0/lib64-msvc-14.1;boost_1_73_0/lib64-msvc-14.0;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/lib;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/../lib;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/stage/lib;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/../lib64-msvc-14.1;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/../lib64-msvc-14.0;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/lib64-msvc-14.1;C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0/lib64-msvc-14.0;C:/local/boost_1_70_0/lib64-msvc-14.1;C:/local/boost_1_70_0/lib64-msvc-14.0;C:/local/boost_1_70/lib64-msvc-14.1;C:/local/boost_1_70/lib64-msvc-14.0;C:/local/boost_1_69_0/lib64-msvc-14.1;C:/local/boost_1_69_0/lib64-msvc-14.0;C:/local/boost_1_69/lib64-msvc-14.1;C:/local/boost_1_69/lib64-msvc-14.0;C:/boost/lib64-msvc-14.1;C:/boost/lib64-msvc-14.0;PATHS;C:/boost/lib;C:/boost;/sw/local/lib
CMake Warning at C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:896 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1018 (_Boost_COMPONENT_DEPENDENCIES)
  C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1694 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:12 (find_package)


-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1833 ] Searching for REGEX_LIBRARY_RELEASE: boost_regex-vc141-mt-x64-1_73;boost_regex-vc141-mt;boost_regex-vc140-mt-x64-1_73;boost_regex-vc140-mt;boost_regex-mt-x64-1_73;boost_regex-mt;boost_regex-mt;boost_regex
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1886 ] Searching for REGEX_LIBRARY_DEBUG: boost_regex-vc141-mt-gd-x64-1_73;boost_regex-vc141-mt-gd;boost_regex-vc140-mt-gd-x64-1_73;boost_regex-vc140-mt-gd;boost_regex-mt-gd-x64-1_73;boost_regex-mt-gd;boost_regex-mt;boost_regex
-- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1960 ] Boost_FOUND = 1
CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:2124 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.73.0

  Boost include path:
  C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0


  Could not find the following Boost libraries:

          boost_regex

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.
Call Stack (most recent call first):
  CMakeLists.txt:12 (find_package)

我注意到有两件事可能是错误的:

  1. 这一行 -- [ C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:1484 ] guessed _boost_COMPILER = -vc141;-vc140 但如果我查看在 boost 构建期间生成的 .lib 文件,它们的名称中有 -vc142
  2. 似乎 cmake 搜索了正确的文件夹,但库文件的名称错误。它使用名称boost_regex-vc141-mt-x64-1_73,但我的boost build startet 中的lib 文件使用lib,例如libboost_regex-vc142-mt-x64-1_73.lib

我通过将-DBoost_COMPILER=-v142 添加到cmake 命令解决了第一个问题。但我无法将Boost_LIB_PREFIX 设置为-DBoost_LIB_PREFIX=lib

【问题讨论】:

  • @MarekR 没错,我的错
  • 根据日志中的路径C:/Users/djc3ho/Documents/Programming/cpp/_Testing/cmake_boost/boost_1_73_0,CMake 至少部分能够在您的位置找到 Boost。问题是 CMake 无法在您的位置找到 boost_regex 库。要调试此问题,请运行 cmake 和附加选项 -DBoost_DEBUG=ON。使用此选项,CMake 将输出被检查的确切文件。将它们与您在安装中实际拥有的进行比较。还要注意警告New Boost version may have incorrect ... - 它可能表明您的问题的核心。
  • @MarekR 我已将提到的问题作为我的问题的基础,但它并没有解决它。我也尝试了完整路径,但它也没有工作
  • 在 CMake 3.14 中,-vc141-vc140 是已知的最高编译器后缀 FindBoost.cmake。例如。在 CMake 3.17.3 中,FindBoost.cmake 了解所有后缀,-vc149-vc148、...-vc140。因此,警告 New Boost version 实际上与您的问题相关:较新版本的 CMake 不需要设置 Boost_COMPILER 变量。 (这绝不是要降低你的调查和答案)。

标签: c++ boost cmake


【解决方案1】:

我找到了解决办法。 正如我在对我的问题所做的最后一次编辑中提到的,编译器的第一个问题可以使用-DBoost_COMPILER=-vc142 选项来修复。 第二个可以通过使用-DBoost_USE_STATIC_LIBS=ON来解决。

所以 CMakeLists 中用于 boost 的 set 块现在是:

set(BOOST_ROOT "boost_1_73_0")
set(Boost_COMPILER "-vc142")
set(Boost_USE_STATIC_LIBS ON)

设置这些变量后,CMake 可以毫无问题地运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2015-11-10
    • 2017-09-25
    • 2021-01-16
    • 1970-01-01
    • 2023-04-04
    • 2020-11-29
    相关资源
    最近更新 更多