Search code examples
c++boostcmakefuture

How to include boost::future in CMake?


I want to use boost::future in my C++ code:

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

Just including the header in the C++ file gives a compilation error:

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

so I tried to include future in the CMakeLists.txt file:

find_package(Boost COMPONENTS future REQUIRED)

However, the make command returns an error:

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")

So how to include future from boost?


Solution

  • You need to:

    #define BOOST_THREAD_PROVIDES_FUTURE
    

    This is documented here

    So you can either use:

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

    Or:

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

    Or:

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