编译Boost ASIO示例时出现错误

时间:2019-07-07 13:30:17

标签: c++ boost g++

我一直在努力尝试从官方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;
                    ^~~~~~~

这是代码中的错误吗?对于那些没有使用这些库的经验的人来说,在存在很多陷阱的情况下很难走。我可以看到还有许多人为此而苦苦挣扎,但就我而言,所有答案似乎都无济于事。我只是愚蠢,还是这个例子有问题?对于长期的工作,我深表歉意。如果有人可以帮我,我将不胜感激,因为我为此付出了很多时间。

1 个答案:

答案 0 :(得分:0)

我在 Windows 和 Mac 上编译时遇到了同样的问题,花了我几个小时才解决,以便其他人在 Visual Studio 和 CMake 上编译时参考补充:

在管理配置的命令参数编辑框中

<块引用>

-DCMAKE_CXX_FLAGS:STRING="/await /EHsc"

对于 Mac,我直接在 cmake 文件上设置变量:

<块引用>

set(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.