【问题标题】:How to include boost::future in CMake?如何在 CMake 中包含 boost::future?
【发布时间】:2020-02-04 07:52:10
【问题描述】:

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

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

只在C++ 文件中包含标头会导致编译错误:

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

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

find_package(Boost COMPONENTS future REQUIRED)

但是,make 命令返回错误:

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 中包含未来?

【问题讨论】:

  • 我想 Boost future 是一个只有标头的库。 COMPONENTS 调用的 COMPONENTS 部分不需要包含 Boost 标头库。
  • 您只需将Boost组件添加到find_package,它们是实际的库。但是 Boost 的许多组件都是仅标头的,因此您可以在源代码中包含标头并使用它。
  • @vre @Simon,你的意思是find_package(Boost COMPONENTS)?如果我这样做,C++ 代码将无法编译:error: ‘future’ in namespace ‘boost’ does not name a template type
  • 您仍然需要在代码中包含未来的标头
  • @AlanBirtles,我编辑了这个问题,它包含在...

标签: c++ boost cmake future


【解决方案1】:

你需要:

#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;

【讨论】:

    猜你喜欢
    • 2022-11-27
    • 2022-12-12
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多