Skip to content
Chad Skeeters edited this page May 6, 2020 · 10 revisions

Clangd is an implementation of the Language Server Protocol leveraging Clang. Clangd’s goal is to provide language “smartness” features like code completion, find references, etc. for clients such as C/C++ Editors.

Installation

Most package managers and major distributions provide a way to install tools from clang-tools-extra which includes Clangd. If you can't find instructions for your workspace below, consider checking Installing Clangd section.

macOS Homebrew:

brew install llvm

For Arch Linux you can just install Clang package which comes with clang-tools-extra:

pacman -Syu clang

Debian-based Linux distributions provide clang-tools package (e.g. Debian package and Ubuntu package). Depending on the version, you may also need the clangd package (Ubuntu Bionic package):

apt-get install clang-tools
apt-get install clangd  # on some distributions

Configuration

let g:LanguageClient_serverCommands = {
  \ 'cpp': ['clangd'],
  \ }

Troubleshooting

If you encounter a bug and you are able to reproduce it, Clangd developers would appreciate a bug submission. Please use LLVM Bugzilla for the bug reports. To help diagnose the problem, you can attach log files and the Mirror File (which stores the LSP input passed to Clangd).

To store stderr output, you can redirect stderr to a file and use LanguageClient#debugInfo() function:

let g:LanguageClient_serverStderr = '/tmp/clangd.stderr'

function SetLSPShortcuts()
  " ...
  " Previous bindings
  " ...
  nnoremap <leader>ll :call LanguageClient#debugInfo()<CR>
endfunction()

Bonus: Building Global Static Index

Clangd can provide global completion when given a global static index. This index is not rebuilt during the editing process which means that if a significant part of the codebase is changed you would have to rebuild it to reflect these changes.

To build the global static index, you can use experimental clangd-indexer tool. If have copied the compile_commands.json to the source directory, call:

clangd-indexer -executor=all-TUs /path/to/project > index.yaml

After the index is produced, you can pass it to Clangd via -index-file:

let g:LanguageClient_serverCommands = {
  \ 'cpp': ['clangd', '-index-file=/path/to/index.yaml',],
  \ }

A project wide index may also be generated like so (assumes compile_commands.json in project root directory, tested on clangd v9):

let g:LanguageClient_serverCommands = {
 \ 'cpp': ['clangd', '-background-index',],
 \ }

Generated index files are placed under the project root.

The global static index infrastructure is experimental, so please be aware that you might encounter some bugs. If you do, please submit a bug report to help make the infrastructure better.

Getting Involved

If you are interested in Clangd development or have any questions, consider using clangd-dev Mailing List.

Clone this wiki locally