c++visual-studio-codecmake

How can I set up C/C++ IntelliSense for a CMake project in VS Code?


I'm trying to write a tool using libTooling. I got it set up so that it compiles with the example from the LLVM Documentation. However C/C++ IntelliSense seems to not work with CMake projects.

My tool is located in:

<project>/clang-tools-extra/mytool

Now the C/C++ extension tries to read the compile_config.json and tells me <project>/build/compile_config.json could not be found, using includePath from c_cpp_properties.json instead.

I tried adding the include paths manually in my workspace settings:

{
    "C_Cpp.default.includePath": [
        "../../",
        "../../clang/Frontend/",
        "../../clang/Tooling/",
        "../../llvm/Support/"
    ],
    "C_Cpp.default.browse.path": [
        "../.."
    ]
}

Or in a file c_cpp_properties.json. But it still searches for the includes in the wrong place. E.g. the include:

#include "llvm/Support/CommandLine.h"

It tries to find in <project>/llvm/include/llvm/Support/CommandLine.h. So apparently it reads something from command_config.json even though it says it can't find it (while it is there), but the wrong thing. It shouldn't add llvm/include at all.


Solution

  • 1) Point '"compileCommands"' to 'compile_commands.json'
    2) You might want to add **, e.g: ../../** double star makes it recursive
    3) You can use some variables like ${workspaceRoot} instead of using relative path in your include paths