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
12To use a binary in a custom location, add the following to your `settings.json`:
13
14```json
15{
16 "lsp": {
17 "clangd": {
18 "binary": {
19 "path": "/path/to/clangd",
20 "arguments": []
21 }
22 }
23 }
24}
25```
26
27If you want to disable Zed looking for a `clangd` binary, you can set `ignore_system_version` to `true`:
28
29```json
30{
31 "lsp": {
32 "clangd": {
33 "binary": {
34 "ignore_system_version": true
35 }
36 }
37 }
38}
39```
40
41## Arguments
42
43You 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.
44
45```json
46{
47 "lsp": {
48 "clangd": {
49 "binary": {
50 "path": "/path/to/clangd",
51 "arguments": ["--function-arg-placeholders=0"]
52 }
53 }
54 }
55}
56```
57
58## Formatting
59
60By 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:
61
62```yaml
63---
64BasedOnStyle: LLVM
65IndentWidth: 4
66---
67Language: Cpp
68# Force pointers to the type for C++.
69DerivePointerAlignment: false
70PointerAlignment: Left
71---
72```
73
74See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
75
76You 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:
77
78```json
79 "languages": {
80 "C++": {
81 "format_on_save": "on",
82 "tab_size": 2
83 }
84 }
85```
86
87## More server configuration
88
89In the root of your project, it is generally common to create a `.clangd` file to set extra configuration.
90
91```text
92CompileFlags:
93 Add:
94 - "--include-directory=/path/to/include"
95Diagnostics:
96 MissingIncludes: Strict
97 UnusedIncludes: Strict
98```
99
100For more advanced usage of clangd configuration file, take a look into their [official page](https://clangd.llvm.org/config.html).
101
102## Compile Commands
103
104For 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.
105
106### CMake Compile Commands
107
108With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
109
110```cmake
111set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
112```
113
114After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.
115
116## Debugging
117
118You 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`.
119
120### Build and Debug Binary
121
122```json
123[
124 {
125 "label": "Debug native binary",
126 "build": {
127 "command": "make",
128 "args": ["-j8"],
129 "cwd": "$ZED_WORKTREE_ROOT"
130 },
131 "program": "$ZED_WORKTREE_ROOT/build/prog",
132 "request": "launch",
133 "adapter": "CodeLLDB"
134 }
135]
136```