Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 652
Deniz Bahadir Avatar asked Aug 16 '18 21:08

Deniz Bahadir


People also ask

Does clangd work with C?

clangd helps developers write, understand and improve C/C++ code by providing: code completion. compile errors and warnings.

How do I get C to work on VS Code?

We need to click on the extension button that displays a sidebar for downloading and installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension. In this image, click on the Install button to install the C/C++ extension.

Why C is not working in VS Code?

Go to the menu Code > Preferences > Settings. In the User tab on the left panel, expand the Extensions section. Find and select Run Code Configuration. Find and check the box Run in Terminal.


1 Answers

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.

enter image description here

like image 90
George Zheng Avatar answered Sep 21 '22 18:09

George Zheng