How to setup VS Code for C++ with clangd support?

Deniz Bahadir picture Deniz Bahadir · Aug 16, 2018 · Viewed 13.9k times · Source

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.

Answer

George Zheng picture George Zheng · Jan 20, 2020

I can share some of my configures.

Microsoft "C/C++" extension is great for debugging, I think you should install it.

MeanWhile, Clangd provide a more accurate result in finding references. So, My sugguestion is keep official C/C++ entension for debugging but disable its intelliSense. Put below lines to your settings.json

    "C_Cpp.intelliSenseEngine": "Disabled",
    "C_Cpp.autocomplete": "Disabled",  // So you don't get autocomplete from both extensions.
    "C_Cpp.errorSquiggles": "Disabled", // So you don't get error squiggles from both extensions (clangd's seem to be more reliable anyway).
    
    "clangd.path": "/path/to/your/clangd",
    "clangd.arguments": ["-log=verbose", "-pretty", "--background-index", "--compile-commands-dir=/path/to/your/compile_commands_dir/"]

When correctly configured, you will see the output of clangd from OUTPUT windows next to Problems and Terminal.

enter image description here