I'm try to learn about Boost::Beast with IDE CLion.
Here is my environment:
- MacBook Pro (15-inch 2018) with Core i9
- Compiler: Apple clang version 11.0.3 (clang-1103.0.32.62) Target: x86_64-apple-darwin19.5.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin
- Clion version: CLion 2020.1.2 Build #CL-201.7846.88, built on June 3, 2020
- Boost: 1.72 installed by HomeBrew
Here is my code:
#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() {}
And the 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)
Everything for build is OK, but the IDE show errors for statement 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...