Search code examples
c++boostboost-asioc++17spawn

How to build a program using boost::asio::spawn


How I can to build a program that uses boost lib?

I can't build an example of use boost::asio:spawn.

  • G++ version: 7.3

  • Boost Lib version: 1.69

Code:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>

int main ()
{
  boost::asio::io_service io_service;

  // Spawn coroutine.
  boost::asio::spawn(io_service,
    [&io_service](boost::asio::yield_context yield_context) {
      // Add more 'work' to the io_service.
      io_service.post([] {
        std::cout << "Other work" << std::endl;
      });

      // Wait on a timer within the coroutine.
      boost::asio::deadline_timer timer(io_service);
      timer.expires_from_now(boost::posix_time::seconds(1));
      std::cout << "Start wait" << std::endl;
      timer.async_wait(yield_context);
      std::cout << "Woke up" << std::endl;
    });

  io_service.run();
}

Error:

boost::asio::experimental::co_spawn has not been declared

Solution

  • I've tried to reproduce your issue, but cannot (using the same versions your report).

    Also see it Live On Wandbox (GCC7.3/boost1.69).

    Here's what I use to build:

    g++ -std=c++11 -pthread -I ~/custom/boost_1_69_0/ -L ~/custom/boost_1_69_0/stage/lib/ test.cpp -lboost_{thread,context,coroutine,system} -DBOOST_COROUTINES_NO_DEPRECATION_WARNING
    

    That's a bash oneliner for:

    g++ -std=c++11 -pthread \ -I /home/sehe/custom/boost_1_69_0/ \ -L /home/sehe/custom/boost_1_69_0/stage/lib/ \ test.cpp \ -lboost_thread \ -lboost_context \ -lboost_coroutine \ -lboost_system \ -DBOOST_COROUTINES_NO_DEPRECATION_WARNING