【问题标题】:CMake Can't Find Boost When Changing Visual Studio Target更改 Visual Studio 目标时 CMake 找不到提升
【发布时间】:2021-02-05 01:38:06
【问题描述】:

我正在遵循 https://badprog.com/c-boost-setting-up-on-windows-10 的指示。我安装了 Visual Studio 2017,并安装了 boost_1_71_0-msvc-14.1-64.exe。 CMake 是从cmake.org/download/cmake-3.19.0-rc1-win64-x64.msi 下载的。

我有一个项目需要 Boost Beast,我相信它是 1.70 或更高版本。

当我运行cmake . 时,一切似乎都很好:

C:\Users\Public\xyz>cmake .
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
-- The CXX compiler identification is MSVC 19.24.28314.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
-- Found Threads: TRUE
-- Found Boost: C:/local/boost_1_71_0 (found suitable version "1.71.0", minimum required is "1.70") found components: thread system chrono date_time atomic
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Public/xyz

生成的项目在 Visual Studio 2017 中打开,但随后抱怨它需要 2019 构建工具。令人惊讶的是,正如您在上面看到的,它是为 Visual Studio 2019 构建的?

但是,在杀死 CMakeCache.txt 和 CMakeFiles 之后,如果我这样做:

C:\Users\Public\xyz>cmake -G "Visual Studio 15 2017" .
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
-- The CXX compiler identification is MSVC 19.16.27034.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
-- Found Threads: TRUE
CMake Error at C:/Program Files/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
  Could NOT find Boost (missing: thread system) (found suitable version
  "1.71.0", minimum required is "1.70")
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:577 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files/CMake/share/cmake-3.19/Modules/FindBoost.cmake:2176 (find_package_handle_standard_args)
  CMakeLists.txt:29 (FIND_PACKAGE)


-- Configuring incomplete, errors occurred!
See also "C:/Users/Public/xyz/CMakeFiles/CMakeOutput.log".

突然找不到 Boost 组件(但可以找到 Boost 吗?)。我尝试了各种形式的-DBOOST_LIBRARYDIR-DBOOST_ROOT等,都无济于事,但我似乎没有cmake找不到Boost的常见stackoverflow问题;相反,它似乎无法识别 Visual Studio 2017 目标,尽管已为 msvc-14.1 安装?

CMakeLists.txt:

# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.1)

# Force C++11, for Boost Beast (async http library) 
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Project's name
project(xyz
        VERSION 0.1
        LANGUAGES CXX)

# Set the output folder where the program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# Specify .cpp and .hpp is in src/
set(PROJECT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)
include_directories("${PROJECT_SOURCE_DIR}")

# Add source to build target for hello world...
add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp)

# Testing http support
ADD_EXECUTABLE( server ${PROJECT_SOURCE_DIR}/server.cpp )

FIND_PACKAGE( Boost 1.70 REQUIRED COMPONENTS thread system )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} )
target_link_libraries ( server LINK_PUBLIC ${Boost_LIBRARIES} )

【问题讨论】:

  • 在 CMake 中调试 Boost 搜索问题的第一件事是启用 Boost_DEBUG 选项。在命令行中,这可以通过将-DBoost_DEBUG=ON 传递给cmake 来实现。使用该选项,您可以查看搜索了哪些确切文件,并将它们与您实际拥有的文件进行比较。
  • 请注意,设置CMAKE_BINARY_DIR 变量是错误:该变量应被视为只读。如果要选择 CMake 将创建构建工件的目录,则直接将此目录用作 构建目录。 (而不是使用cmake . 执行源内构建)。变量PROJECT_SOURCE_DIR 也不应该更改。
  • 感谢齐瓦列夫!对于-DBoost_DEBUG=ON,我发现由于某种原因它默认为 32 位架构。 cmake -G "Visual Studio 15 2017" -DBoost_DEBUG=ON -DBoost_ARCHITECTURE=-x64 -DBOOST_LIBRARYDIR=C:\local\boost_1_71_0\lib64-msvc-14.1 . 工作! ...但 MSVC 仍然无法链接,出于某种原因寻找 32 位。是否有一个简单的“处处使用 64 位”标志?
  • 最后一个问题的答案是更改生成器 - cmake -G "Visual Studio 15 2017 Win64" . - 这些工具肯定有很多陷阱;)

标签: c++ visual-studio boost cmake


【解决方案1】:

最后,问题很简单。如果可以的话,对于初学者来说这是一个容易犯的错误,但对于初学者来说几乎不可能通过常规的谷歌搜索来解开。

当我发布 cmake . 时,它默认为 64 位构建,但假设是 Visual Studio 2019 目标。那是错误的。

等效的应该是cmake -G "Visual Studio 15 2017 Win64" . 但不知道这一点,并且按照错误的教程,我改为发出cmake -G "Visual Studio 15 2017" .。这默认为 32 位构建 - 尽管默认情况下它从未提及这一事实。它可以找到boost,但是所有的库文件都是64位的,32位的没用,所以它只是说“我找不到任何东西!”。

感谢@Tsyvarev,感谢他的回答。打开 DEBUG 并逐字符查看搜索到的库文件确实是正确的选择。

【讨论】:

    猜你喜欢
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2021-09-20
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多