Issue
I use CMake FetchContent to download nlohmann/json. But my clangd doesn't scan the library after downloading. So how should I configure my clangd?
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)
and my code 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;
}
my clangd says:
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'
Solution
Now I know how to solve this problem.
When using CMake, set CMAKE_EXPORT_COMPILE_COMMANDS
to 1, to make CMake generate the file compile_commands.json
. Clangd will automatically scan this file and follow it to scan for third-party libraries.
Answered By - earthmessenger Answer Checked By - Marilyn (WPSolving Volunteer)