Clion显示错误,但可以使用Cmake成功构建代码

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

本文关键字:Cmake 成功 构建 代码 可以使 显示 错误 Clion      更新时间:2023-10-16

我正在尝试使用 IDE CLion 了解 Boost::Beast。

这是我的环境:

  • MacBook Pro(15英寸2018(与酷睿i9
  • 编译器: 苹果叮当版本 11.0.3 (clang-1103.0.32.62( 目标:x86_64苹果达尔文19.5.0 螺纹型号:磅 已安装目录:/Library/Developer/CommandLineTools/usr/bin
  • Clion 版本:CLion 2020.1.2 内部版本 #CL-201.7846.88,构建于 2020 年 6 月 3 日
  • 提升: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...

你需要告诉CLion你的工具链。具体来说,CLion 应该使用 CMake 编译数据库。我想这通常应该"自动"发生,但显然它不同步。

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

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

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

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

经过大量的尝试,我产生了一些想法:

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。叮当声 10 显示上面的错误,但叮当声 9 没有。

因此,在这种情况下,似乎在 Clion 支持替代 clangd 或 Boost 找出兼容性问题之前,问题无法解决。

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