FindPackageHandleStandardArgs.cmake:137 处的 CMake 错误(消息):找不到 Boost(缺少:正则表达式)(找到合适的版本“1.72.0”,
Posted
技术标签:
【中文标题】FindPackageHandleStandardArgs.cmake:137 处的 CMake 错误(消息):找不到 Boost(缺少:正则表达式)(找到合适的版本“1.72.0”,【英文标题】:CMake Error at FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find Boost (missing: regex) (found suitable version "1.72.0", 【发布时间】:2020-02-15 22:15:24 【问题描述】:我从官方网站下载了 boost_1_72_0.tar.gz 并将其解压缩到我的下载文件夹/USERS/macuser/Downloads/boost_1_72_0
尝试使用 Boost::regex 链接简单的 .cpp 文件时,我不断收到以下错误
-- The C compiler identification is AppleClang 11.0.0.11000033
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:1144 (message):
New Boost version may have incorrect or missing dependencies and imported
targets
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:1266 (_Boost_COMPONENT_DEPENDENCIES)
/usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:1904 (_Boost_MISSING_DEPENDENCIES)
examples/HelloBoost/CMakeLists.txt:27 (find_package)
CMake Error at /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find Boost (missing: regex) (found suitable version "1.72.0",
minimum required is "1.72.0")
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
/usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:2161 (find_package_handle_standard_args)
examples/HelloBoost/CMakeLists.txt:27 (find_package)
我的 CMakeLists.txt 文件如下所示:
cmake_minimum_required(VERSION 3.1)
set (CMAKE_CXX_STANDARD 11)
set (PROJECT "Hello-Boost")
# headers
include_directories(include)
# sources
file(GLOB SOURCES "src/*.cpp")
# BOOST Configuration
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_INCLUDE_DIR /USERS/macuser/Downloads/boost_1_72_0/boost)
set(Boost_LIBRARY_DIR /USERS/macuser/Downloads/boost_1_72_0/libs)
find_package(Boost 1.72.0 COMPONENTS regex REQUIRED)
if(Boost_FOUND)
message("boost lib: $Boost_LIBRARIES")
message("boost inc:$Boost_INCLUDE_DIR")
emdif()
include_directories($Boost_INCLUDE_DIR)
link_directories($Boost_LIBRARY_DIR)
# executable target
add_executable($PROJECT $SOURCES)
# link boost
target_link_libraries($PROJECT PUBLIC Boost::regex)
我的问题很简单: 以上帝的名义,Cmake 出了什么问题? 当数以百万计的其他人成功地做到了,为什么它却找不到该死的 boost 库?
谢谢
【问题讨论】:
你构建了 Boost::regex 吗? boost.org/doc/libs/1_72_0/libs/regex/doc/html/boost_regex/… 对cmake
使用-DBoost_DEBUG=ON
选项,您可能会发现哪个文件被搜索为Regex Boost 库。知道了这一点,您可以检查您是否确实拥有此文件。此外,最好通过更新 CMake 来“修复”警告 New Boost version may have incorrect or missing dependencies and imported targets
。该警告字面意思是说您的 Boost 对于 CMake(或者更准确地说,对于 FindBoost.cmake
脚本)来说太新了,有些事情可能无法正常工作。
【参考方案1】:
在使用Boost::regex
之前,您需要构建 Boost,因为它不是一个只有标头的库(与大多数其他 Boost 库一样)。这通常在安装过程中通过软件包管理器(如 homebrew 或 macports)完成。
仅标题库不需要添加到您的find_package
调用的COMPONENTS
部分。
然后你做一个
find_package(BOOST 1.72.0 COMPONENTS regex REQUIRED)
就像你已经做的那样,然后通过
将库添加到你的目标中target_link_libraries($PROJECT PUBLIC Boost::regex)
如果您打算稍后使用其他 Boost 库(仅或不使用标头),您应该在 target_link_libraries 调用中明确命名它们或添加
target_link_libraries($PROJECT PUBLIC $Boost_LIBRARIES)
这也会将 Boost 目录添加到您的包含目录中。 然后您可以省略以下行
include_directories($Boost_INCLUDE_DIR)
link_directories($Boost_LIBRARY_DIR)
如果您想使用静态库而不是动态库,请使用
set(Boost_USE_STATIC_LIBS ON)
在您拨打find_package
之前。
【讨论】:
我现在无法链接,尽管生成和配置工作正常 [80%] 构建 CXX 对象示例/HelloBoost/CMakeFiles/Hello-Boost.dir/src/main.cpp.o [100%] 链接 CXX 可执行 Hello-Boost 架构 x86_64 的未定义符号: "boost::regex_error::regex_error(std::__1::basic_string以上是关于FindPackageHandleStandardArgs.cmake:137 处的 CMake 错误(消息):找不到 Boost(缺少:正则表达式)(找到合适的版本“1.72.0”,的主要内容,如果未能解决你的问题,请参考以下文章