【问题标题】:Clion show errors but the codes can be built successfully with CmakeClion 显示错误,但可以使用 Cmake 成功构建代码
【发布时间】: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


    【解决方案1】:

    您需要告诉 CLion 您的工具链。具体来说,CLion 应该使用 CMake compilation database。我想这通常应该“自动”发生,但显然它不同步。

    您也可以选择“不同”的完成引擎

    • 设置/首选项 |编辑 |一般 |代码完成

    具体来说,最近的 CLion 版本旨在解决您所描述的一致性问题:https://blog.jetbrains.com/clion/2020/04/clion-2020-1-cuda-clang-embedded/

    在 2020.1 迭代期间,我们完善了 Clang 提供的补全(通过修复数十个相关问题并添加缺失的补全功能),最终在 CLion 中启用了 Clang 作为代码补全的唯一提供者的模式作为默认值。这解决了一些优先级和排序问题。

    【讨论】:

      【解决方案2】:

      经过大量尝试,我想到了一些想法:

      Clion 2020.1 使用特定的 clangd(v10.0) 来完成代码,但最新的 clangd 并不完全兼容 Boost::Asio/Boost::Beast 1.72 的某些 C++20 语法,例如协程支持。我不知道起源是什么,可能是 Boost::Beast/Asio 的兼容性问题,或者只是最新的 clangd 的一个错误。

      我在 Visual Studio Code 中使用 clangd 10 和 clangd 9 尝试了我的想法,因为 Clion 不支持替代 clangd。 clangd 10 显示上述错误,但 clangd 9 没有。

      所以在这种情况下,似乎直到Clion支持替代clangd或Boost找出兼容性问题后才能解决问题。

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

      【讨论】:

        猜你喜欢
        • 2019-07-31
        • 1970-01-01
        • 2013-03-14
        • 2013-06-20
        • 2020-10-23
        • 2018-03-11
        • 2019-04-17
        • 2012-05-18
        • 1970-01-01
        相关资源
        最近更新 更多