c++ - 编译 Boost ASIO 示例时出错
问题描述
我已经挣扎了好几个小时,试图从官方的 boost 主页上运行这个例子:
#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 <cstdio>
using boost::asio::ip::tcp;
using boost::asio::awaitable;
using boost::asio::co_spawn;
using boost::asio::detached;
using boost::asio::use_awaitable;
namespace this_coro = boost::asio::this_coro;
awaitable<void> echo(tcp::socket socket)
{
try
{
char data[1024];
for (;;)
{
std::size_t n = co_await socket.async_read_some(boost::asio::buffer(data), use_awaitable);
co_await async_write(socket, boost::asio::buffer(data, n), use_awaitable);
}
}
catch (std::exception& e)
{
std::printf("echo Exception: %s\n", e.what());
}
}
awaitable<void> listener()
{
auto executor = co_await this_coro::executor;
tcp::acceptor acceptor(executor, {tcp::v4(), 55555});
for (;;)
{
tcp::socket socket = co_await acceptor.async_accept(use_awaitable);
co_spawn(executor,
[socket = std::move(socket)]() mutable
{
return echo(std::move(socket));
},
detached);
}
}
int main()
{
try
{
boost::asio::io_context io_context(1);
boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
signals.async_wait([&](auto, auto){ io_context.stop(); });
co_spawn(io_context, listener, detached);
io_context.run();
}
catch (std::exception& e)
{
std::printf("Exception: %s\n", e.what());
}
}
我在 Linux 上并将源代码保存到/usr/local/boost_1_70_0
. 我阅读了我必须包含的文档<boost/system/error_code>
和<boost/system/system_error>
(为什么代码示例中没有这个?)。在 StackOverflow 上大量阅读之后,我发现我也应该使用-lboost_system
,但是编译器只是抱怨它找不到它。另一个技巧是使用-lpthread
,现在它不再抱怨了。我尝试的最后一个命令是
g++ -I /usr/local/boost_1_70_0 main.cpp -lpthread
但是,它说:
error: ‘boost::asio::awaitable’ has not been declared
using boost::asio::awaitable;
^~~~~~~
这是代码中的错误吗?当有这么多陷阱时,对于没有使用这些库的经验的人来说,这是非常困难的。我可以看到还有很多其他人在为此苦苦挣扎,对我的情况似乎没有任何答案。我只是愚蠢,还是这个例子有问题?对于这篇长篇文章我感到抱歉,如果有人能帮我一把,我将永远感激不尽,因为我已经为此苦苦挣扎了好几个小时。
解决方案
我在 Windows 和 Mac 上编译时遇到了同样的问题,我花了几个小时来解决它,所以作为其他人在 Visual Studio 和 CMake 上编译的参考添加:
在管理配置的命令参数编辑框中
-DCMAKE_CXX_FLAGS:STRING="/await /EHsc"
对于 Mac,我直接在 cmake 文件上设置变量:
设置(CMAKE_CXX_FLAGS“-fcoroutines-ts”)
使用 gcc 似乎要使用的标志是“-fcoroutines”
参考完整的 cmake 文件可能很有用:
# CMakeList.txt : CMake project for TGFLocalClient, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
set(CMAKE_CXX_STANDARD 17)
# Boost section
# set(Boost_DEBUG "ON")
set(Boost_LIB_PREFIX "lib")
set(BOOST_ROOT "C:/Users/Andrea/boost_1_75_0/boost_1_75_0")
set(BOOST_LIBRARIES "date_time" "regex")
find_package(Boost COMPONENTS ${BOOST_LIBRARIES})
message(STATUS "${BOOST_ROOT}")
message(STATUS "${BOOST_FOUND}")
message(STATUS "${Boost_INCLUDE_DIRS}")
message(STATUS "${Boost_LIBRARY_DIRS}")
message(STATUS "${BOOST_LIBRARIES}")
message("${Boost_regex_FOUND}")
message("${Boost_regex_LIBRARY}")
include_directories("${Boost_INCLUDE_DIRS}")
# Add source to this project's executable.
add_executable (TGFLocalClient "TGFLocalClient.cpp" "TGFLocalClient.h" "ServiceDiscovery.h" "NetProtocol.h")
target_link_libraries(TGFLocalClient Boost::date_time Boost::regex)
# TODO: Add tests and install targets if needed.
推荐阅读
- reactjs - 不知道如何在这里实现条件渲染(react-native)
- mysql - GROUP BY 根据每列的不同条件
- floating-point - 数据类型浮点数的准确性(IEEE 754 单精度)
- python - 如何在 Python 中使用 Selenium 在 Google 上进行搜索?
- tarantool - 所有 Tarantool 节点 (vshard) 的可用空间
- routes - 如何从 osmnx 导出路由
- c++ - 通过 Freertos 中的队列发送字符串
- nestjs - 在没有 moduleRef.get 的情况下获得服务的可能性
- asp.net-core - 使用 OpenIdDict 时不允许使用“offline_access”范围
- powershell - 仅当某些变量不为空时,如何使用 Powershell 发送电子邮件?