如何使用boost :: asio :: spawn构建程序

时间:2018-12-20 13:49:46

标签: c++ boost boost-asio c++17 spawn

如何构建使用boost lib的程序?

我无法建立使用boost::asio:spawn的示例。

  • G ++版本:7.3

  • Boost Lib版本:1.69

代码:

#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();
}

错误:

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

1 个答案:

答案 0 :(得分:0)

我尝试重现您的问题,但不能(使用与您报告相同的版本)。

也可以看到它Live On Wandbox (GCC7.3/boost1.69)

这就是我用来构建的东西:

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

这是bash oneliner:

  

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