【问题标题】:boost::asio::co_spawn is undefined in MSVCboost::asio::co_spawn 在 MSVC 中未定义
【发布时间】:2019-05-09 07:14:27
【问题描述】:

我正在尝试使用 boost.asio 创建 TCP 服务器,并按照您使用 co_spawn 的示例在新线程中启动侦听器函数。

当我尝试使用 boost::asio::co_spawn 时,Microsoft Visual Studios 告诉我它未定义,我已经包含了 boost example 所做的所有文件。

TcpServer.h

#include <iostream>
#include <string>

#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/coroutine.hpp>

using namespace boost::asio;

class TcpServer
{
public:
    TcpServer(int port);
    ~TcpServer();
private:

};

TcpServer.cpp

#include "TcpServer.h"


/*
    Create a TcpServer on a given port
*/
TcpServer::TcpServer(int port)
{
    io_context ioCtx(1);

    /* E0020    identifier "co_spawn" is undefined  */
    co_spawn();
}


TcpServer::~TcpServer()
{
}

上面的代码告诉我co_spawn 未定义,我搜索了其他名称空间并检查了替代函数,但没有。我想也许它是过时的文档,但1.70.1 版本的boost.asio 参考仍然将co_spawn 列为免费功能。见here

我正在使用 提升:1.70.1 Microsoft Visual Studio 2017:v15.9.7

【问题讨论】:

  • 你需要在 msvc 上的命令行选项来启用协程吗?

标签: c++ boost boost-asio asio


【解决方案1】:

boost.asio 有预处理器代码来检测编译器的功能,并且只有在编译器支持时才打开协程支持。

对于 MSVC,我认为您需要将编译器标志 /await 添加到编译器命令行选项列表中。

参考:https://docs.microsoft.com/en-us/cpp/build/reference/await-enable-coroutine-support?view=vs-2019

【讨论】:

  • 谢谢您,效果很好,文档链接非常有帮助!我很惊讶我在所有的谷歌搜索和文档阅读中都找不到这方面的东西。
  • @ImpossibleMushroom 我只知道在哪里看,因为我昨天碰巧阅读了一些新的 ASIO 源代码,因为我也想使用 co_routines 来简化我们的生产代码。
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 2021-07-24
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多