21

Disclaimer: I am totally new 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.

Deniz Bahadir
  • 503
  • 1
  • 4
  • 16

2 Answers2

45

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/"]

Note: The directory /path/to/your/compile_commands_dir/ should have a file compile_commands.json.

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

A. K.
  • 34,395
  • 15
  • 52
  • 89
George Zheng
  • 574
  • 4
  • 7
  • I have "clangd.arguments" configured but it is blurred in settings.json, what does that mean? – frogEye May 26 '21 at 13:33
  • @frogEye It normally means this feature is outdated or doesn't exist, I'm not sure why it's your case because this feature surely exists and not outdated. Can you try to add another line of config `"clangd.checkUpdates": true` and see how's it going? – George Zheng May 28 '21 at 02:27
  • Thanks @George Zheng, I figured out the all issue was due to opening the workspace. The extension was not working with workspace but when I tried to open my code as a folder it worked. – frogEye Jun 01 '21 at 12:51
  • Few days back, i came to know about LSP and clangd. While configuring it, i came across the term "comple-commands" but i can't understand what is does as my LSP is working without it. Can you please tell me a bit about it and how can i get it for myself. I am using gcc compiler for c++ – Gaurav Oct 04 '21 at 15:22
  • @GauravAggarwal as its name suggested, it's a database in json file to inform clangd how to process it. if you are using cmake you and enable it by `set(CMAKE_EXPORT_COMPILE_COMMANDS ON)`. if not you can try this project [Bear](https://github.com/rizsotto/Bear) – George Zheng Oct 12 '21 at 07:31
  • FYI, if `"C_Cpp.intelliSenseEngine": "Disabled"`, then the `"C_Cpp.autocomplete"` and `"C_Cpp.errorSquiggles"` settings aren't needed since [they'll be ignored](https://github.com/microsoft/vscode-cpptools/issues/4979#issuecomment-723224009). – Mark G Nov 23 '21 at 17:00
  • I see `clangd` in the drop down of the output window but not Clang language server like yours. Is that ok too or is somethinh wrong? – roulette01 Dec 11 '21 at 01:44
  • 1
    @roulette01 it's correct. my snapshot is outdated. – George Zheng Dec 13 '21 at 08:38
  • For line "--compile-commands-dir=/path/to/your/compile_commands_dir/"] , how I can fill ? – Tiến TeoCri Aug 27 '22 at 09:27
6

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.

Jan Joneš
  • 777
  • 6
  • 13