c.md

 1# C
 2
 3C support is available natively in Zed.
 4
 5- Tree-sitter: [tree-sitter/tree-sitter-c](https://github.com/tree-sitter/tree-sitter-c)
 6- Language Server: [clangd/clangd](https://github.com/clangd/clangd)
 7- Debug Adapter: [CodeLLDB](https://github.com/vadimcn) (primary), [GDB](https://sourceware.org/gdb/) (secondary, not available on Apple silicon)
 8
 9## Clangd: Force detect as C
10
11Clangd out of the box assumes mixed C++/C projects. If you have a C-only project you may wish to instruct clangd to treat all files as C using the `-xc` flag. To do this, create a `.clangd` file in the root of your project with the following:
12
13```yaml
14# yaml-language-server: $schema=https://json.schemastore.org/clangd.json
15CompileFlags:
16  Add: [-xc]
17```
18
19By default clang and gcc will recognize `*.C` and `*.H` (uppercase extensions) as C++ and not C and so Zed too follows this convention. If you are working with a C-only project (perhaps one with legacy uppercase pathing like `FILENAME.C`) you can override this behavior by adding this to your settings:
20
21```json [settings]
22{
23  "file_types": {
24    "C": ["C", "H"]
25  }
26}
27```
28
29## Formatting
30
31By default Zed will use the `clangd` language server for formatting C code like the `clang-format` CLI tool. To configure this you can add a `.clang-format` file. For example:
32
33```yaml
34# yaml-language-server: $schema=https://json.schemastore.org/clang-format-21.x.json
35---
36BasedOnStyle: GNU
37IndentWidth: 2
38---
39```
40
41See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
42
43You 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:
44
45```json [settings]
46  "languages": {
47    "C": {
48      "format_on_save": "on",
49      "tab_size": 2
50    }
51  }
52```
53
54## Compile Commands
55
56For 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.
57
58### CMake Compile Commands
59
60With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
61
62```cmake
63set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
64```
65
66After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.
67
68## Debugging
69
70You 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`.
71
72- [CodeLLDB configuration documentation](https://github.com/vadimcn/codelldb/blob/master/MANUAL.md#starting-a-new-debug-session)
73- [GDB configuration documentation](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Debugger-Adapter-Protocol.html)
74
75### Build and Debug Binary
76
77```json [debug]
78[
79  {
80    "label": "Debug native binary",
81    "build": {
82      "command": "make",
83      "args": ["-j8"],
84      "cwd": "$ZED_WORKTREE_ROOT"
85    },
86    "program": "$ZED_WORKTREE_ROOT/build/prog",
87    "request": "launch",
88    "adapter": "CodeLLDB"
89  }
90]
91```