Set Up Your Code Editor
In this section, we will discuss how to set up a development environment using Vim or VSCode, both need to configure a language server and install a bookmark plug-in.
Important
If you are using VSCode in a host machine and running a Linux distro or FreeBSD in a VM or a container, you may need to set up VSCode Remote Development Suite to allow VSCode to access the development environment via SSH.
Since VSCode’s Remote SSH Extension doesn’t officially support FreeBSD, it requires some extra tricks to get it working. You can refer to this gist to do this.
Tip
If you have trouble setting up Vim or VSCode Remote Development Suite, consider using Helix. This is a full-featured, batteries-included TUI code editor, and you can install it via a single command on most of the Linux distros and FreeBSD.
Once you’ve installed and configured a language server, you no longer need to spend any effort configuring Helix. It just works.
Language Server
A language server is a program that provides language-specific features like code completion, linting, and syntax highlighting to development environments through the Language Server Protocol. There are 2 actively maintained language servers of C/C++:
- clangd: Maintained by LLVM
- ccls: Maintained by Fangrui Song
Both of them will read a file in the root of a project named compile_commands.json
, which provides clangd and ccls compilation information so they can correctly analyze the source code. We’ll talk about how to generate this file later.
Take clangd for example, there are 3 steps to get it working:
- Install clangd on your machine: Installing clangd
Note
The package contains clangd
binary in FreeBSD 14.2.0 is llvm19
, and you may need to update $PATH
like this to use it: export PATH="/usr/local/llvm19/bin:$PATH"
.
- Install code editor plug-in: Editor plugins
- Configure clangd to use the following arguments:
clangd \
--background-index \
--header-insertion=never
--background-index
tells clangd to index project in the background and persist index on disk. This is very important because the kernel’s codebase is very large, and it’ll take a long time to index, so you generally don’t want clangd to recreate index every time you open the project.
--header-insertion=never
tells clangd not to insert headers on completion. We need this because clangd will falsely insert unnecessary headers.
You can apply these arguments via your editor’s settings or a configuration file.
To list all available arguments of clangd, execute clang --help
. Personally, I use the following arguments:
clangd \
--background-index \
--header-insertion=never \
--clang-tidy \
--completion-style=detailed
Alternatively, if you want to use ccls instead of clangd, follow the instructions on the project’s wiki page: ccls wiki
Bookmark
This book relies on editors’ bookmark plug-in to mark mentioned code, so you need to install and set up the bookmark plug-in in your editor.
Install MattesGroeger/vim-bookmarks and add let g:bookmark_save_per_working_dir = 1
to your vimrc, so bookmarks will be stored in a file named .vim-bookmarks
in the current working directory.
Then you can download the bookmark file and put it in the project root as .vim-bookmark
, where the project is the source code of the kernels that we’ll obtain later. Reopen your code editor, and you can see the bookmarks.
The download URLs are listed below:
Install alefragnani.Bookmarks and add "bookmarks.saveBookmarksInProject": true
to your settings.json, so bookmarks will be stored in a file named .vscode/bookmarks.json
in the current working directory.
Then you can download the bookmark file and put it in the project root as .vscode/bookmarks.json
, where the project is the source code of the kernels that we’ll obtain later. Reopen your code editor, and you can see the bookmarks.
The download URLs are listed below: