【问题标题】:How should I configure clangd to make it scan the library I download with CMake FetchContent?我应该如何配置 clangd 以使其扫描我使用 CMake FetchContent 下载的库?
【发布时间】:2022-08-21 12:14:55
【问题描述】:

我使用 CMake FetchContent 下载 nlohmann/json。但是我的clangd下载后没有扫描库。那么我应该如何配置我的clangd?

我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.11)
project(ExampleProject LANGUAGES CXX)

include(FetchContent)

FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz)
FetchContent_MakeAvailable(json)

add_executable(example main.cc)
target_link_libraries(example PRIVATE nlohmann_json::nlohmann_json)

和我的代码 main.cc:

#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
    json object = { { \"one\", 1 }, { \"two\", 2 } };
    std::cout << object << \'\\n\';
    return 0;
}

我的叮当 说:

main.cc|2 col 10-29 error| \'nlohmann/json.hpp\' file not found
main.cc|4 col 14-22 error| Use of undeclared identifier \'nlohmann\'
main.cc|8 col 5-9 error| Unknown type name \'json\'

    标签: c++ cmake


    【解决方案1】:

    现在我知道如何解决这个问题了。

    使用 CMake 时,将CMAKE_EXPORT_COMPILE_COMMANDS 设置为 1,使 CMake 生成文件compile_commands.json。 Clangd 会自动扫描这个文件,并按照它来扫描第三方库。

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多