【发布时间】:2020-10-22 18:35:57
【问题描述】:
我正在尝试使用 IDE CLion 了解 Boost::Beast。
这是我的环境:
- 配备酷睿 i9 的 MacBook Pro(15 英寸 2018 年)
- 编译器: 苹果 clang 版本 11.0.3 (clang-1103.0.32.62) 目标:x86_64-apple-darwin19.5.0 线程模型:posix 安装目录:/Library/Developer/CommandLineTools/usr/bin
- Clion 版本:CLion 2020.1.2 Build #CL-201.7846.88,于 2020 年 6 月 3 日构建
- Boost:由 HomeBrew 安装的 1.72
这是我的代码:
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <iostream>
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = net::ip::tcp;
using boost::asio::awaitable;
using boost::asio::use_awaitable;
awaitable<void> test() {
net::io_context ctx;
tcp::resolver resolver(ctx);
http::response<http::dynamic_body> res;
auto const results =
co_await
resolver.async_resolve("www.google.com", "80", use_awaitable);
beast::tcp_stream stream(ctx);
// Set the timeout.
stream.expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup
co_await
stream.async_connect(results, use_awaitable);
// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, "/", 5};
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Set the timeout.
stream.expires_after(std::chrono::seconds(30));
// Send the HTTP request to the remote host
co_await http::async_write(stream, req, use_awaitable);
}
int main() {}
还有 CMkaeLists.txt
cmake_minimum_required(VERSION 3.16)
project(untitled)
if(APPLE)
include_directories(/usr/local/include)
endif()
find_package(Boost REQUIRED)
set(CMAKE_CXX_STANDARD 20)
add_executable(untitled main.cpp)
构建一切正常,但 IDE 显示语句 co_await http::async_write(stream, req, use_awaitable); 的错误:
Function 'async_write<boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::executor, boost::beast::unlimited_rate_policy>, true, boost::beast::http::basic_string_body<char, std::__1::char_traits<char>, std::__1::allocator<char> >, boost::beast::http::basic_fields<std::__1::allocator<char> >, const boost::asio::use_awaitable_t<boost::asio::executor> &>' with deduced return type cannot be used before it is defined 'async_write<boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::executor, boost::beast::unlimited_rate_policy>, true, boost::beast::http::basic_string_body<char, std::__1::char_traits<char>, std::__1::allocator<char> >, boost...
【问题讨论】:
标签: c++ boost cmake boost-beast