c++ - boost::asio::co_spawn 在 MSVC 中未定义
问题描述
我正在尝试使用 boost.asio 创建一个 TCP 服务器,并按照您使用 co_spawn 的示例在新线程中启动侦听器函数。
当我尝试使用boost::asio::co_spawn
Microsoft 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
解决方案
boost.asio 有预处理器代码来检测编译器的功能,并且只有在编译器支持时才打开协程支持。
对于 MSVC,我认为您需要将编译器标志添加到编译/await
器命令行选项列表中。
参考:https ://docs.microsoft.com/en-us/cpp/build/reference/await-enable-coroutine-support?view=vs-2019
推荐阅读
- google-cloud-platform - 更新我的项目的范围后,谷歌身份验证令牌会起作用吗?
- javascript - 为什么说我不能设置属性 fontSize 因为某些东西是未定义的?
- windows - 如何在不重定向到 WOW6432Node 的情况下使用 32 位 Powershell 访问 64 位注册表项
- laravel-5 - 在 2 模型中使用 try catch 时如何修复 laravel 中的回滚事务?
- swift - 检查用户是否已经在 SwiftyStoreKit 中购买了产品
- python-3.x - 如何从 python3 中解开 python 2 中的 sklearn.tree.DescisionTreeRegressor
- eslint - ESLint 规则强制对单参数箭头函数不使用括号
- docker - 创建服务失败并显示“未指定命令”
- python - 如何从最后一行开始对时间序列数据进行反向重采样?
- unity3d - 如何实施“可见性系统”