编译Boost ASIO示例时出现错误
Errors when compiling Boost ASIO examples
我一直在挣扎数小时,试图从官方的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.
- boost::进程间消息队列引发错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在cygwin中测试新的boost安装时出现cpp错误
- 错误"Could not find Boost"(缺少:上下文标头)
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- VS Code C++:不准确的系统包括路径错误(wchar.h,boost/lambda/lambda.hpp)
- 尝试将 boost::stacktrace 添加到 CMake 项目时出现构建错误
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- 使用boost::multiprecision cpp_int左移时出现超时错误
- 致命错误:找不到'boost/uuid/uuid.hpp'文件
- 造成致命错误:boost/fusion/iterator/equal_to.hpp 没有这样的文件或目录
- 编译器错误boost :: asio ::占位符:: boost v1.69的错误
- C++链接错误(Boost线程)
- 错误:“boost::array”不是一种类型
- 开始使用boost-cpu_timer时,错误:“boost::timer::cpu_timer”尚未声明
- 编译错误-boost::numeric::ublas::coordinate_matrix
- Boost错误:Boost.文件系统V3及更高版本需要std::wstring支持
- 错误boost::文件系统没有名为string()的成员
- 错误:BOOST禁用线程
- 链接错误Boost.使用stlport作为标准库的Python