我一直在挣扎数小时,试图从官方的Boost Homepage中获得此示例以运行:
#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: %sn", 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: %sn", 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 ='/afate/ehsc&quot; quot;
对于Mac I直接设置CMAKE文件上的变量:
set(cmake_cxx_flags; quot; fcoroutines-ts&quort;(
使用GCC,似乎要使用的标志是" -fcoroutines&quot"
它可能是参考完整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.