Cmake doesn't find Boost

I'm trying to configure a project using CMake, but it fails to find Boost libraries even though they are in the specified folder. I have specified Boost_INCLUDE_DIR, Boost_LIBRARYDIR and BOOST_ROOT , but I still get an error saying that CMake is not able to find Boost. What could be the reason of such error?

239760 次浏览

你确定你这样做是正确的吗?其思想是 CMake 自动设置 BOOST_INCLUDE_DIRBOOST_LIBRARYDIRBOOST_ROOT。在 CMakeLists.txt中执行以下操作:

FIND_PACKAGE(Boost)
IF (Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()

如果升级没有安装在默认位置,因此 CMake 找不到它,你可以告诉 CMake 在哪里寻找升级,如下所示:

SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/win32libs/boost")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/win32libs/boost/lib")

当然,这两条线必须是 之前CMakeLists.txt中的 FIND_PACKAGE(Boost)

通过读取 FindBoost.cmake文件本身还有更多的帮助。它位于您的“模块”目录中。

一个良好的开端是 set(Boost_DEBUG 1)-这将吐出大量的信息,关于在哪里提高正在寻找,它正在寻找,并可能有助于解释为什么它无法找到它。

它还可以帮助你弄清楚它是否正确地拾取你的 BOOST_ROOT

如果在可用版本变量中没有列出确切的升压版本,FindBoost.cmake有时也会出现问题。你可以通过阅读 FindBoost.cmake找到更多关于这方面的信息。

最后,FindBoost.cmake在过去也有一些 bug。你可以尝试从最新版本的 CMake 中提取一个新版本的 FindBoost.cmake,然后把它和 CMakeLists.txt一起放到你的项目文件夹中——那么即使你有一个旧版本的升级,它也会使用你项目文件夹中的新版本 FindBoost.cmake

祝你好运。

对于那些试图使用静态链接 Boost 构建 CGAL的人,还有一点建议。仅仅定义 Boost_USE_STATIC_LIBS是不够的; 当 Boost_DEBUG输出它的值时,它会被覆盖。这里要做的事情是勾选“ Advanced”复选框并启用 CGAL_Boost_USE_STATIC_LIBS

如果您正在构建自己的提升,不要忘记使用 --layout=versioned,否则搜索某个特定版本的库将会失败

我也遇到了类似的问题,发现 BOOST _ INCLUDE _ DIR、 BOOST _ LIBRARYDIR 和 BOOST _ ROOT env 变量必须保存 绝对的路径。 哈!

For cmake version 3.1.0-rc2 to pick up boost 1.57 specify -D_boost_TEST_VERSIONS=1.57

如使用 -DBoost_DEBUG=ON所示,cmake version 3.1.0-rc2默认为 boost<=1.56.0

cmake -D_boost_TEST_VERSIONS=1.57 -DBoost_DEBUG=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

I struggled with this problem for a while myself. It turned out that cmake was looking for Boost library files using Boost's naming convention, in which the library name is a function of the compiler version used to build it. Our Boost libraries were built using GCC 4.9.1, and that compiler version was in fact present on our system; however, GCC 4.4.7 also happened to be installed. As it happens, cmake's FindBoost.cmake script was auto-detecting the GCC 4.4.7 installation instead of the GCC 4.9.1 one, and thus was looking for Boost library files with "gcc44" in the file names, rather than "gcc49".

简单的修复方法是通过在 CMakeLists.txt中将 Boost_COMPILER设置为“ -gcc49”,强制 cmake 假设存在 GCC 4.9。通过这个更改,FindBoost.cmake查找并找到了我的 Boost 库文件。

您还可以通过传递指向正确版本升级头的位置的 -DBOOST_INCLUDEDIR-DBOOST_ROOT来指定希望 CMake 使用的升级版本

例如:

cmake -DBOOST_ROOT=/opt/latestboost

当多个升级版本在同一个系统上时,这也很有用。

在 Ubuntu Trusty64上安装 Boost 版本1.66.0之后,我在为一个项目运行 make时遇到了同样的问题。错误消息类似(不完全一样) :

CMake Error at
/usr/local/Cellar/cmake/3.3.2/share/cmake/Modules/FindBoost.cmake:1245 (message):
Unable to find the requested Boost libraries.
Boost version: 0.0.0
Boost include path: /usr/include
Detected version of Boost is too old.  Requested version was 1.36 (or newer).
Call Stack (most recent call first):
CMakeLists.txt:10 (FIND_PACKAGE)

Boost 肯定已经安装了,但是 CMake 无法检测到它。在花了大量时间修补路径和环境变量之后,我最终检查了 cmake本身的选项,发现了以下内容:

--check-system-vars        = Find problems with variable usage in system files

因此,我在有问题的目录中运行以下命令:

sudo cmake --check-system-vars

它返回了:

Also check system files when warning about unused and uninitialized variables.
-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   system
--   filesystem
--   thread
--   date_time
--   chrono
--   regex
--   serialization
--   program_options
-- Found Git: /usr/bin/git
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/myproject

解决了这个问题。

也许吧

brew install boost

会帮助你。

我也遇到了同样的问题,以上的解决方案都不管用。实际上,无法读取文件 include/boost/version.hpp(由 Jenkins 启动的 cmake 脚本读取)。

我不得不手动使用(升级)库的 更改许可(尽管 Jenkins 属于这个组,但这是另一个我无法解决的与 Jenkins 相关的问题) :

chmod o+wx ${BOOST_ROOT} -R # allow reading/execution on the whole library
#chmod g+wx ${BOOST_ROOT} -R # this did not suffice, strangely, but it is another story I guess

如果将 CMAKE_FIND_ROOT_PATH设置为与 BOOST_ROOT不同,也会发生这种情况。 我面临同样的问题,尽管设置 BOOST_ROOT,我得到了错误。 但是对于 ARM 的交叉编译,我使用的是 Toolchain-android. cmake,其中(出于某种原因) :

set(BOOST_ROOT "/home/.../boost")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot=${SYSROOT}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/include/libcxx")
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
set(CMAKE_FIND_ROOT_PATH "${SYSROOT}")

CMAKE_FIND_ROOT_PATH似乎覆盖了引起问题的 BOOST_ROOT

对我来说,这个错误仅仅是因为在 ubuntu 上没有安装升级:

sudo apt install build-essential libboost-system-dev libboost-thread-dev libboost-program-options-dev libboost-test-dev

在我的情况下,Boost 没有安装。我在 Mac 上使用了以下命令,然后 cmake find _ package (Boost)工作起来非常有效

brew install Boost

请注意 Boost 中大写的“ B”!

请先看 FindBoost.cmake。您设置的变量是正确的,但它们都应该是大写的。

确保库架构与 CMake 配置匹配。

cmake -A x64 ..

我建议创建一个最小的可执行文件,其中只包含一个 Boost 库,以查看它是否编译。

#include <iostream>
#include <boost/date_time.hpp>
int main() {
using namespace std;
using namespace boost::gregorian;
date today = day_clock::local_day();
cout << today << endl;
}
find_package(Boost REQUIRED COMPONENTS
date_time
)


include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})


add_executable(test_boost "test_boost.cpp")
target_link_libraries(test_boost Boost::date_time)

首先检查 Boost_FOUND开始调试。

message(STATUS "Boost_FOUND: ${Boost_FOUND}")

即使没有找到库,也应该找到该版本

如果 Boost_LIBRARY_DIRS变为非空,则应该编译它。

对于那些使用 python3.7码头图像的人来说,这解决了:

apt install libboost-system-dev libboost-thread-dev