cpp.md

  1# C++
  2
  3C++ support is available natively in Zed.
  4
  5- Tree-sitter: [tree-sitter/tree-sitter-cpp](https://github.com/tree-sitter/tree-sitter-cpp)
  6- Language Server: [clangd/clangd](https://github.com/clangd/clangd)
  7
  8## Binary
  9
 10You can configure which `clangd` binary Zed should use.
 11
 12By 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.
 13
 14If 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`:
 15
 16```json [settings]
 17{
 18  "lsp": {
 19    "clangd": {
 20      "fetch": {
 21        "pre_release": true
 22      }
 23    }
 24  }
 25}
 26```
 27
 28If you want to disable Zed looking for a `clangd` binary, you can set `ignore_system_version` to `true` in your `settings.json`:
 29
 30```json [settings]
 31{
 32  "lsp": {
 33    "clangd": {
 34      "binary": {
 35        "ignore_system_version": true
 36      }
 37    }
 38  }
 39}
 40```
 41
 42If you want to use a binary in a custom location, you can specify a `path` and optional `arguments`:
 43
 44```json [settings]
 45{
 46  "lsp": {
 47    "clangd": {
 48      "binary": {
 49        "path": "/path/to/clangd",
 50        "arguments": []
 51      }
 52    }
 53  }
 54}
 55```
 56
 57This `"path"` has to be an absolute path.
 58
 59## Arguments
 60
 61You 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.
 62
 63```json [settings]
 64{
 65  "lsp": {
 66    "clangd": {
 67      "binary": {
 68        "path": "/path/to/clangd",
 69        "arguments": ["--function-arg-placeholders=0"]
 70      }
 71    }
 72  }
 73}
 74```
 75
 76## Formatting
 77
 78By 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:
 79
 80```yaml
 81# yaml-language-server: $schema=https://json.schemastore.org/clang-format-21.x.json
 82---
 83BasedOnStyle: LLVM
 84IndentWidth: 4
 85---
 86Language: Cpp
 87# Force pointers to the type for C++.
 88DerivePointerAlignment: false
 89PointerAlignment: Left
 90---
 91```
 92
 93See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
 94
 95You can trigger formatting via {#kb editor::Format} or the `editor: format` action from the command palette or by adding `format_on_save` to your Zed settings:
 96
 97```json [settings]
 98  "languages": {
 99    "C++": {
100      "format_on_save": "on",
101      "tab_size": 2
102    }
103  }
104```
105
106## More server configuration
107
108In the root of your project, it is generally common to create a `.clangd` file to set extra configuration.
109
110```yaml
111# yaml-language-server: $schema=https://json.schemastore.org/clangd.json
112CompileFlags:
113  Add:
114    - "--include-directory=/path/to/include"
115Diagnostics:
116  MissingIncludes: Strict
117  UnusedIncludes: Strict
118```
119
120For more advanced usage of clangd configuration file, take a look into their [official page](https://clangd.llvm.org/config.html).
121
122## Compile Commands
123
124For 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.
125
126### CMake Compile Commands
127
128With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
129
130```cmake
131set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
132```
133
134After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.
135
136## Debugging
137
138You 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`.
139
140- [CodeLLDB configuration documentation](https://github.com/vadimcn/codelldb/blob/master/MANUAL.md#starting-a-new-debug-session)
141- [GDB configuration documentation](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Debugger-Adapter-Protocol.html)
142  - GDB needs to be at least v14.1
143
144### Build and Debug Binary
145
146```json [debug]
147[
148  {
149    "label": "Debug native binary",
150    "build": {
151      "command": "make",
152      "args": ["-j8"],
153      "cwd": "$ZED_WORKTREE_ROOT"
154    },
155    "program": "$ZED_WORKTREE_ROOT/build/prog",
156    "request": "launch",
157    "adapter": "CodeLLDB"
158  }
159]
160```