LinuxCommandLibrary

clangd

Provide C/C++ language server features

TLDR

Display available options

$ clangd --help
copy

List of available options
$ clangd --help-list
copy

Display version
$ clangd --version
copy

SYNOPSIS

clangd [options] [file1 file2 ...]

PARAMETERS

--background-index
    Index the project in the background, allowing other operations to proceed concurrently.

--clang-tidy
    Enable clang-tidy integration for static analysis.

--compile-commands-dir=
    Path to the directory containing compile_commands.json (defaults to the source file's directory or its parents).

--j=
    Number of threads to use for indexing.

--log=
    Set the logging level (e.g., verbose, error, warning).

--query-driver=
    Specify a driver command to query for compiler arguments.

DESCRIPTION

Clangd is a language server that provides IDE-like features (code completion, diagnostics, go-to-definition, find-references, etc.) for C, C++, Objective-C, and Objective-C++ code.

It is designed to be used with editors and IDEs that support the Language Server Protocol (LSP). Instead of integrating compiler functionality directly into the editor, clangd runs as a separate process and communicates with the editor using the LSP. This allows a single clangd instance to serve multiple editors and IDEs, and also keeps the editor lightweight and responsive.

Clangd leverages the Clang compiler infrastructure, providing accurate and up-to-date information about the code. It uses compile_commands.json to discover compilation flags and include paths. Setting up this file is crucial for accurate results.

It efficiently indexes code, caching results to provide a fast user experience. It can handle large codebases.

CAVEATS

Clangd requires a `compile_commands.json` file to be present in the project, otherwise, it relies on default compiler flags, which can lead to inaccurate results.

The quality of the results depends on the accuracy of the compilation database.

CONFIGURATION

Clangd is configured primarily through the `compile_commands.json` file, but also supports `.clangd` configuration files within your project to configure settings specific to that project or parts of it. The server behavior can be further modified through client settings passed by the editor.

PERFORMANCE

To ensure optimal performance, it is recommended to use a fast storage device for the project's source code and the clangd index. Increase the number of threads used for indexing with the `--j` parameter can also speed up indexing, given sufficient cores. Properly configured caching can also reduce the need to reindex on every change.

HISTORY

Clangd was developed as part of the LLVM project to provide a high-performance, accurate, and feature-rich language server for C/C++/Objective-C. Its development is ongoing, with new features and improvements being added regularly.

It is used by various editors and IDEs, including VS Code, Vim/Neovim (via plugins), and Emacs.

SEE ALSO

Copied to clipboard