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
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. The Clangd is the same as the `clang-format` CLI tool. To configure this you can add a `.clang-format` file. For example:
31
32```yaml
33---
34BasedOnStyle: GNU
35IndentWidth: 2
36---
37```
38
39See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
40
41You 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:
42
43```json
44 "languages": {
45 "C": {
46 "format_on_save": "on",
47 "tab_size": 2
48 }
49 }
50```
51
52## Compile Commands
53
54For 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.
55
56### CMake Compile Commands
57
58With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
59
60```cmake
61set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
62```
63
64After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.
65
66## Debugging
67
68You 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`.
69
70### Build and Debug Binary
71
72```json
73[
74 {
75 "label": "Debug native binary",
76 "build": {
77 "command": "make",
78 "args": ["-j8"],
79 "cwd": "$ZED_WORKTREE_ROOT"
80 },
81 "program": "$ZED_WORKTREE_ROOT/build/prog",
82 "request": "launch",
83 "adapter": "CodeLLDB"
84 }
85]
86```