Skip to content

C

C support is built in.

Feature Provided by
Language server clangd
Formatting clang-format, through clangd
Debugging CodeLLDB or GDB

Tyrn uses clangd from your PATH if it finds one, and downloads one otherwise.

Tell clangd how your code builds

clangd needs to know your compiler flags. Give it a compile_commands.json at the project root, or in a build folder. With CMake, generate it with:

Terminal window
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

Other build tools can produce one with a tool such as bear. Extra flags and per-folder rules go in a .clangd file in your project.

Formatting

clangd formats with clang-format. Put a .clang-format file in your project to set the style:

.clang-format
BasedOnStyle: LLVM
IndentWidth: 2

Format with cmd-shift-i. To format on save, and to set the tab size for C:

settings.json
{
"languages": {
"C": { "format_on_save": "on", "tab_size": 2 }
}
}

Use a different clangd

settings.json
{
"lsp": {
"clangd": {
"binary": {
"path": "/opt/homebrew/opt/llvm/bin/clangd",
"arguments": ["--header-insertion=never"]
}
}
}
}

binary.path must be absolute. Use "fetch": { "pre_release": true } to download pre-release builds, and binary.ignore_system_version to ignore a clangd on your PATH.

Switch between source and header

The editor has a Switch Source Header action, editor: switch source header. It has no default key. Bind one:

keymap.json
[
{
"context": "Editor",
"bindings": { "alt-enter": "editor::SwitchSourceHeader" }
}
]

Debugging

Add a configuration to .tyrn/debug.json that builds first, then launches your binary:

.tyrn/debug.json
[
{
"label": "Debug native binary",
"adapter": "CodeLLDB",
"request": "launch",
"program": "./build/prog",
"build": { "command": "make", "args": ["-j8"] }
}
]

Use "adapter": "GDB" if you prefer gdb, which must be installed. See Debugger.