'How to setup VS Code for C++ with clangd support?
Disclaimer: I am totally knew to VS Code, so, please, be gentle with me. :-)
I am trying to set up VS Code for C++.
However, I explicitly want to set it up so that it uses the Language Server Protocol to communicate with clangd
when handling C++-files.
I already installed clangd
on my (Ubuntu Linux) system and the official "vscode-clangd" extension from the VS Code market, and I also adjusted its settings so that clangd
should be found by it.
However, now I am lost.
When I open a *.cpp
or *.hpp
file VS Code recommends some other extensions to me (e.g. the official Microsoft "C/C++" extension with IntelliSense support) but I do not see where and how clangd
does help me at all.
Using Microsoft's "C/C++" extension seems to work out of the box but how can I use clangd
?
Thanks for any help.
Solution 1:[1]
I can share some of my configures.
Microsoft "C/C++" extension is great for debugging, I think you should install it.
Meanwhile, Clangd provides a more accurate result in finding references. So, My suggestion is to keep the official C/C++ extension for debugging but disable its IntelliSense. Put below lines to your settings.json
"C_Cpp.intelliSenseEngine": "Disabled",
"clangd.path": "/path/to/your/clangd",
"clangd.arguments": ["-log=verbose",
"-pretty",
"--background-index",
//"--query-driver=/bin/arm-buildroot-linux-gnueabihf-g++", //for cross compile usage
"--compile-commands-dir=/path/to/your/compile_commands_dir/"]
Always refer to the official website, there are more settings like filtering out compile param etc. When correctly configured, you will see the output of clangd from OUTPUT windows next to Problems and Terminal.
Solution 2:[2]
It should work without any configuration.
I have tested that on Windows and it works just fine—I have no C/C++ extension installed in Visual Studio Code, just vscode-clangd
and it reports errors, provides code completion, etc.
That means the extension works, because there are no such features in the "core" Visual Studio Code.
Visual Studio Code does still suggest popular C/C++ extensions, but you can ignore that, it doesn't mean that vscode-clangd
isn't working.
Note that the file you are editing has to have a standard extension like .cpp
or .c
to be recognized and acted upon by vscode-clangd
.
See the extension's source code for the list of all supported extensions.
For simple projects, having no configuration may be enough, but for more complex ones, you will of course need to let Clang know things like include directories, compilation flags, etc.
This can be done by creating a compile_flags.txt
file where you type arguments for Clang, one per line.
You can put this file into the same folder as your source files or anywhere up the tree.
After editing this file, you have to restart Visual Studio Code, so that the changes take effect.
Alternatively, you can create (or let CMake generate) a compile_commands.json
file.
It has the following syntax:
[
{ "directory": "/home/user/llvm/build",
"command": "/usr/bin/clang++ -Irelative -DSOMEDEF=\"With spaces, quotes and \\-es.\" -c -o file.o file.cc",
"file": "file.cc" },
...
]
See Clang docs for more details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Jan Joneš |