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
14CompileFlags:
15  Add: [-xc]
16```
17
18By 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:
19
20```json [settings]
21{
22  "file_types": {
23    "C": ["C", "H"]
24  }
25}
26```
27
28## Formatting
29
30By 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:
31
32```yaml
33# yaml-language-server: $schema=https://json.schemastore.org/clang-format-21.x.json
34---
35BasedOnStyle: GNU
36IndentWidth: 2
37---
38```
39
40See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
41
42You 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:
43
44```json [settings]
45  "languages": {
46    "C": {
47      "format_on_save": "on",
48      "tab_size": 2
49    }
50  }
51```
52
53## Compile Commands
54
55For 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.
56
57### CMake Compile Commands
58
59With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
60
61```cmake
62set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
63```
64
65After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.
66
67## Debugging
68
69You 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`.
70
71- [CodeLLDB configuration documentation](https://github.com/vadimcn/codelldb/blob/master/MANUAL.md#starting-a-new-debug-session)
72- [GDB configuration documentation](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Debugger-Adapter-Protocol.html)
73
74### Build and Debug Binary
75
76```json [debug]
77[
78  {
79    "label": "Debug native binary",
80    "build": {
81      "command": "make",
82      "args": ["-j8"],
83      "cwd": "$ZED_WORKTREE_ROOT"
84    },
85    "program": "$ZED_WORKTREE_ROOT/build/prog",
86    "request": "launch",
87    "adapter": "CodeLLDB"
88  }
89]
90```