如何在 CMake 中包含 boost::future?

How to include boost::future in CMake?

我想在我的 C++ 代码中使用 boost::future

#include <boost/thread/future.hpp>
boost::future<int> f...

仅在 C++ 文件中包含 header 会出现编译错误:

error: ‘future’ in namespace ‘boost’ does not name a template type

所以我尝试将 future 包含在 CMakeLists.txt 文件中:

find_package(Boost COMPONENTS future REQUIRED)

但是,make命令returns出错:

CMake Error at /usr/local/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find Boost (missing: future) (found version "1.71.0")

那么如何从 boost 中包含 future 呢?

您需要:

#define BOOST_THREAD_PROVIDES_FUTURE

这已记录在案 here

因此您可以使用:

#define BOOST_THREAD_PROVIDES_FUTURE
#include <boost/thread/future.hpp>
boost::future<int> f;

或:

#include <boost/thread/future.hpp>
boost::unique_future<int> f;

或:

// any version >= 3 will work
#define BOOST_THREAD_VERSION 5
#include <boost/thread/future.hpp>
boost::future<int> f;