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---
82BasedOnStyle: LLVM
83IndentWidth: 4
84---
85Language: Cpp
86# Force pointers to the type for C++.
87DerivePointerAlignment: false
88PointerAlignment: Left
89---
90```
91
92See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
93
94You 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:
95
96```json [settings]
97 "languages": {
98 "C++": {
99 "format_on_save": "on",
100 "tab_size": 2
101 }
102 }
103```
104
105## More server configuration
106
107In the root of your project, it is generally common to create a `.clangd` file to set extra configuration.
108
109```text
110CompileFlags:
111 Add:
112 - "--include-directory=/path/to/include"
113Diagnostics:
114 MissingIncludes: Strict
115 UnusedIncludes: Strict
116```
117
118For more advanced usage of clangd configuration file, take a look into their [official page](https://clangd.llvm.org/config.html).
119
120## Compile Commands
121
122For 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.
123
124### CMake Compile Commands
125
126With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
127
128```cmake
129set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
130```
131
132After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.
133
134## Debugging
135
136You 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`.
137
138- [CodeLLDB configuration documentation](https://github.com/vadimcn/codelldb/blob/master/MANUAL.md#starting-a-new-debug-session)
139- [GDB configuration documentation](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Debugger-Adapter-Protocol.html)
140
141### Build and Debug Binary
142
143```json [debug]
144[
145 {
146 "label": "Debug native binary",
147 "build": {
148 "command": "make",
149 "args": ["-j8"],
150 "cwd": "$ZED_WORKTREE_ROOT"
151 },
152 "program": "$ZED_WORKTREE_ROOT/build/prog",
153 "request": "launch",
154 "adapter": "CodeLLDB"
155 }
156]
157```