Teo
Teo

Reputation: 23

how to configure CMake project so that clangd extension can see system headers in VS Code?

I have a setup of VS Code in windows 10 using MinGW for c++ development and I want to use the clangd extension for code completion and insights, the problem I'm having is that clangd can't find system headers.

reading the guide in clangd's website they recommend when using MinGW on Windows to use --query-driver over specifying system include paths. I haven't been able to correctly do this.

I am generating the compile_commands.json file that clangd needs using Cmake. this is how my config looks:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.19.0)
project(SimpleTestProj VERSION 0.1.0 LANGUAGES C CXX)

include(CTest)
enable_testing()

add_executable(SimpleTestProj test.cpp)
set_property(TARGET SimpleTestProj PROPERTY CXX_STANDARD 17)

set(CMAKE_C_COMPILER "C:/msys64/ucrt64/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/msys64/ucrt64/bin/g++.exe")
set(CMAKE_LINKER "C:/msys64/ucrt64/bin/ld.exe")

CMakePresets.json

{
  "version": 3,
  "configurePresets": [
    {
      "name": "default",
      "displayName": "Default Config",
      "description": "Default settings for my cmake projects",
      "hidden": false,
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug",
        "CMAKE_EXPORT_COMPILE_COMMANDS": "YES",
        "CMAKE_C_COMPILER": "C:/msys64/ucrt64/bin/gcc.exe",
        "CMAKE_CXX_COMPILER": "C:/msys64/ucrt64/bin/g++.exe",
        "CMAKE_LINKER": "C:/msys64/ucrt64/bin/ld.exe"
      }
    }
  ]
}

reading the FAQ and the troubleshooting guide in the clangd page, they list two options to solve this, one is by manually adding the path of the headers in a config.yaml, and while this does allow clangd to see the headers it now complains about bits/c++config.h file missing, which is a whole other problem.

the solution they recommend over the previous one is to pass the flag --query-driver=/path/to/mygcc,/path/to/myg++ to allow clangd to extract the include paths directly. but I have no idea how to do that, I tried adding the line "CMAKE_CXX_FLAGS": "--query-driver=/path/to/mygcc,/path/to/myg++" to CMakePresets.json, but CMake doesn't recognize the command.

I've also confirmed the output of clangd in VS Code and it does load the compile_commands.json correctly.

what is the correct way to pass the flag and get clangd working?

Thanks everyone.

Upvotes: 0

Views: 402

Answers (0)

Related Questions