Setting up a C IDE for an Autotools Project

Coming from the world of interpreted languages (Python, Javascript, PHP) I am used to just opening a project in an IDE and having it Just Work™. I'm currently teaching myself to code in C, and have spent more time than I would like to admit just getting an IDE set up to autocomplete and catch syntax errors. In this post, I will go over how I managed to get VSCodium set up with a C language server, and how to get it to recognise imported libraries. Hopefully this post can help someone else from having the same struggles, or just be useful to my future self if I take a break.

Build Systems

Many people complain that Python has too many ways of handling dependencies - pip, pyenv, poetry, pipenv. It seems to me that C has the same problem. There's cmake, meson, autotools, ninja, and probably more. Some of these will come with tools to generate the necessary files for an IDE to work with the project, but not all of them. Some of them also require learning their DSL, which I'm not interested in doing before I can start a project.

The majority of projects I am interested in diving into are using Autotools, which is not one of the build systems which generates compile_commands.json. This is what caused me so much annoyance when trying to set up an IDE.

How the IDEs / Language Servers Work

The standard for the Language Servers I have looked at seems to be a file called compile_commands.json, which is typically auto-generated by the build process. This contains the steps to build the application, letting the Language Server know where to look for libraries and headers.

In order to generate this for a project which doesn't do it automatically, we can use a tool called Bear:

Bear on Github.

This is a project-agnostic tool to generate the compile_commands.json file automatically from a regular Makefile.

Setting up the IDE

For this example I will be using VSCodium. The steps in this post should, however, apply to any IDE using ccls or clangd as a Language Server.

Set up CCLS

Grab CCLS and install it as per the instructions. On Fedora Silverblue 36, this involves building it from source (in a toolbox). I then used the "Shell script wrapper" instructions to do a pretend "install" to /usr/bin.

Add CCLS to VSCodium

Install the vscode-ccls extension.

Generating compile_commands.json in an Autotools Project

Now that the language server is installed and integrated, we can go ahead and begin building our project. I will use the current instructions from OSTree as an example, but this should work for any Autotools project.

git submodule update --init
env NOCONFIGURE=1 ./autogen.sh
./configure --prefix=...
make
make install DESTDIR=/path/to/dest

Begin by following the instructions up to the point where we run make. For our example, this is the git command, the autogen.sh script, and the ./configure.

Now we run make but pass it through bear, like so:

bear -- make

This will run the Makefile and generate compile_commands.json for us. Nifty!

Import the Project Into the IDE

With this done, we should be able to open the project in VSCodium and let ccls provide us with auto-complete and syntax checking!

Useful Links