RIN.OKAB3
RIN.OKAB3

Reputation: 51

Clion show errors but the codes can be built successfully with Cmake

I'm try to learn about Boost::Beast with IDE CLion.

Here is my environment:

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

Upvotes: 2

Views: 901

Answers (2)

RIN.OKAB3
RIN.OKAB3

Reputation: 51

After a ton of tries, some ideas come to me:

Clion 2020.1 use specific clangd(v10.0) for code completion but the latest clangd is not totally compatible with Boost::Asio/Boost::Beast 1.72 for some C++20 syntax such as the coroutine support. I have no idea about what is the origin, maybe it is a Boost::Beast/Asio compatibility problem or just a bug for the latest clangd.

I tried my idea in my Visual Studio Code with clangd 10 and clangd 9, because Clion does not support to alternative clangd. The clangd 10 shows errors above but the clangd 9 does not.

So as this situation, it seems like the issue can not be solved until the Clion supports to alternative clangd or the Boost figure out the compatibility problem.

https://youtrack.jetbrains.com/issue/CPP-18725#focus=streamItem-27-3903547.0-0

Upvotes: 0

sehe
sehe

Reputation: 392833

You need to tell CLion about your tool-chain. Specifically, CLion should use the CMake compilation database. This should normally happen "automagically" I suppose, but clearly it is out of synch.

It's also possible that you select a "different" completion engine

  • Settings/Preferences | Editor | General | Code Completion

Specifically, recent CLion versions aimed to fix consistency issues like the one you desribe: https://blog.jetbrains.com/clion/2020/04/clion-2020-1-cuda-clang-embedded/

During the 2020.1 iteration, we polished the completion provided by Clang (by fixing dozens of related issues and adding missing completion features) and finally enabled the mode in which Clang is the only provider for code completion in CLion as the default. This resolves some prioritization and ordering issues.

Upvotes: 0

Related Questions