1---
2title: C++
3description: "Configure C++ language support in Zed, including language servers, formatting, and debugging."
4---
5
6# C++
7
8C++ support is available natively in Zed.
9
10- Tree-sitter: [tree-sitter/tree-sitter-cpp](https://github.com/tree-sitter/tree-sitter-cpp)
11- Language Server: [clangd/clangd](https://github.com/clangd/clangd)
12
13## Binary
14
15You can configure which `clangd` binary Zed should use.
16
17By default, Zed will try to find a `clangd` in your `$PATH` and try to use that. If that binary successfully executes, it's used. Otherwise, Zed will fall back to installing its own `clangd` version and use that.
18
19If you want to install a pre-release `clangd` version instead you can instruct Zed to do so by setting `pre_release` to `true` in your `settings.json`:
20
21```json [settings]
22{
23 "lsp": {
24 "clangd": {
25 "fetch": {
26 "pre_release": true
27 }
28 }
29 }
30}
31```
32
33If you want to disable Zed looking for a `clangd` binary, you can set `ignore_system_version` to `true` in your `settings.json`:
34
35```json [settings]
36{
37 "lsp": {
38 "clangd": {
39 "binary": {
40 "ignore_system_version": true
41 }
42 }
43 }
44}
45```
46
47If you want to use a binary in a custom location, you can specify a `path` and optional `arguments`:
48
49```json [settings]
50{
51 "lsp": {
52 "clangd": {
53 "binary": {
54 "path": "/path/to/clangd",
55 "arguments": []
56 }
57 }
58 }
59}
60```
61
62This `"path"` has to be an absolute path.
63
64## Arguments
65
66You can pass any number of arguments to clangd. To see a full set of available options, run `clangd --help` from the command line. For example with `--function-arg-placeholders=0` completions contain only parentheses for function calls, while the default (`--function-arg-placeholders=1`) completions also contain placeholders for method parameters.
67
68```json [settings]
69{
70 "lsp": {
71 "clangd": {
72 "binary": {
73 "path": "/path/to/clangd",
74 "arguments": ["--function-arg-placeholders=0"]
75 }
76 }
77 }
78}
79```
80
81## Formatting
82
83By default Zed will use the `clangd` language server for formatting C++ code. The Clangd is the same as the `clang-format` CLI tool. To configure this you can add a `.clang-format` file. For example:
84
85```yaml
86# yaml-language-server: $schema=https://json.schemastore.org/clang-format-21.x.json
87---
88BasedOnStyle: LLVM
89IndentWidth: 4
90---
91Language: Cpp
92# Force pointers to the type for C++.
93DerivePointerAlignment: false
94PointerAlignment: Left
95---
96```
97
98See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
99
100You can trigger formatting via {#kb editor::Format} or the `editor: format` action from the command palette or by enabling format on save.
101
102Configure formatting in Settings ({#kb zed::OpenSettings}) under Languages > C++, or add to your settings file:
103
104```json [settings]
105 "languages": {
106 "C++": {
107 "format_on_save": "on",
108 "tab_size": 2
109 }
110 }
111```
112
113## More server configuration
114
115In the root of your project, it is generally common to create a `.clangd` file to set extra configuration.
116
117```yaml
118# yaml-language-server: $schema=https://json.schemastore.org/clangd.json
119CompileFlags:
120 Add:
121 - "--include-directory=/path/to/include"
122Diagnostics:
123 MissingIncludes: Strict
124 UnusedIncludes: Strict
125```
126
127For more advanced usage of clangd configuration file, take a look into their [official page](https://clangd.llvm.org/config.html).
128
129## Compile Commands
130
131For some projects Clangd requires a `compile_commands.json` file to properly analyze your project. This file contains the compilation database that tells clangd how your project should be built.
132
133### CMake Compile Commands
134
135With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
136
137```cmake
138set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
139```
140
141After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.
142
143## Debugging
144
145You can use CodeLLDB or GDB to debug native binaries. (Make sure that your build process passes `-g` to the C++ compiler, so that debug information is included in the resulting binary.) See below for examples of debug configurations that you can add to `.zed/debug.json`.
146
147- [CodeLLDB configuration documentation](https://github.com/vadimcn/codelldb/blob/master/MANUAL.md#starting-a-new-debug-session)
148- [GDB configuration documentation](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Debugger-Adapter-Protocol.html)
149 - GDB needs to be at least v14.1
150
151### Build and Debug Binary
152
153```json [debug]
154[
155 {
156 "label": "Debug native binary",
157 "build": {
158 "command": "make",
159 "args": ["-j8"],
160 "cwd": "$ZED_WORKTREE_ROOT"
161 },
162 "program": "$ZED_WORKTREE_ROOT/build/prog",
163 "request": "launch",
164 "adapter": "CodeLLDB"
165 }
166]
167```
168
169## Protocol Extensions
170
171Zed currently implements the following `clangd` [extensions](https://clangd.llvm.org/extensions):
172
173### Inactive Regions
174
175Automatically dims inactive sections of code due to preprocessor directives, such as `#if`, `#ifdef`, or `#ifndef` blocks that evaluate to false.
176
177### Switch Between Source and Header Files
178
179Allows switching between corresponding C++ source files (e.g., `.cpp`) and header files (e.g., `.h`).
180by running the command {#action editor::SwitchSourceHeader} from the command palette or by setting
181a keybinding for the `editor::SwitchSourceHeader` action.
182
183```json [keymap]
184{
185 "context": "Editor",
186 "bindings": {
187 "alt-enter": "editor::SwitchSourceHeader"
188 }
189}
190```