首页 > 解决方案 > boost::asio::co_spawn 在 MSVC 中未定义

问题描述

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

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

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是未定义的,我搜索了其他名称空间并检查了替代函数,但没有。我想也许它是过时的文档,但boost.asio版本的参考1.70.1仍然co_spawn列为免费功能。看这里

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

标签: c++boostboost-asioasio

解决方案


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

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

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


推荐阅读