15

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.

5 Answers 5

22

For VSCode 1.63+ (2022 or later):

1. Install the CMake Tools extension (ms-vscode.cmake-tools).

2. Put this in .vscode/c_cpp_properties.json:

{ "configurations": [ { "name": "CMake", "compileCommands": "${config:cmake.buildDirectory}/compile_commands.json", "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4 } 

With that said, there is now an alternative C++ language server based on clangd. It uses the actual compiler instead of heuristics and so can infer template types and other complex concepts better. See also this related question for more details.

Sign up to request clarification or add additional context in comments.

Comments

9

If you're using the CMake Tools extension

You need to configure the Cpptools extension to use the CMake tools extensions as the configuration provider.

If you don't have a need to define multiple Cpptools c_cpp configurations, I'd just go and put the following in your workspace .vscode/settings.json:

"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", 

Otherwise, you can create a c_cpp configuration in your .vscode/c_cpp_configurations.json and set configurationProvider in there. Ex. (put this in the configurations array property)

{ "name": "CMake Tools", "configurationProvider": "ms-vscode.cmake-tools" } 

You may or may not need to run configuration and build for IntelliSense to catch up to your changes. I'm not exactly sure about the implementation details or historical nuances on that. From what I've observed, problem detection is build-diagnostic-based, so you'll need to rebuild to "refresh" problem detection for things like error squiggles in the editor.

That's it. If you're the kind of person who likes reading docs (crazy, right? that people like that exist?)...

CMake Tools docs for setting up intellisense can be found here. Quoting from it:

CMake Tools currently supports Microsoft's ms-vscode.cpptools extension. If the ms-vscode.cpptools extension is installed and enabled, then configuring your project will provide this integration automatically.

ms-vscode.cpptools will show a prompt confirming that you wish to use CMake Tools to provide the configuration information for your project. Accept this prompt to activate the integration. Subsequently, CMake Tools will provide and automatically update cpptools configuration information for each source file in your project.

The Cpptools FAQ entry on setting up IntelliSense can be found here. Quoting from it:

Or, if you install a build system extension that interfaces with our extension, you can allow that extension to provide the configurations for you. For example, the CMake Tools extension can configure projects that use the CMake build system. Use the C/C++: Change Configuration Provider... command to enable any such extension to provide the configurations for IntelliSense.

Cpptools docs for the configurationProvider setting can be found here. Quoting from it:

configurationProvider: The ID of a VS Code extension that can provide IntelliSense configuration information for source files. For example, use the VS Code extension ID ms-vscode.cmake-tools to provide configuration information from the CMake Tools extension. If you have specified a configurationProvider, the configurations that provides will take precedence over your other settings in c_cpp_properties.json.

Several other answers written here suggest that you should also set the compileCommands property of your c_cpp configuration. I have not found that to be necessary in my own experience. If I understand correctly, that is just another separate way to get IntelliSense with the Cpptools extension. Again, quoting from the Cpptools FAQ entry on setting up IntelliSense:

A third option for projects without build system extension support is to use a compile_commands.json file if your build system supports generating this file. In the "Advanced" section of the Configuration UI, you can supply the path to your compile_commands.json and the extension will use the compilation information listed in that file to configure IntelliSense.

Emphasis on "A third option". Also, CMake doesn't currently support creating compile command database files for all the generators it supports. See the docs for CMAKE_EXPORT_COMPILE_COMMANDS, which states:

Note: This option is implemented only by Makefile Generators and Ninja Generators. It is ignored on other generators.

So using that is not quite the most portable approach.

If you're using the Clangd extension

I've already written about this elsewhere: How can I make the VS Code clangd extension aware of the include paths defined in my CMake configuration?. TL;DR if you aren't doing in-source builds, you may need to tell clangd where your compile_commands.json file is.

Comments

5

You need to install cmake-tool extension (ms-vscode.cmake-tools) and add following configuration to your c_cpp_properties.json:

{ "configurations": [ { "compileCommands": "${workspaceFolder}/_build/compile_commands.json", "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4 } 

It is working for me

Comments

4

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

1 Comment

Hi pooya13, thank you for those tips. I think they improve the configuration. I figured it out in the meantime (it was MSVC that was failing, it works with clang). I accepted this answer anyway, because it's additional help.
1

Maybe you just need to enable the extension cmake-tools, configure the CMakeLists.txt correctly, and then run cmake.

Normally, as long as there's no error with cmake, Intellisense should work properly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.