Stm32CubeIDE and Vim on Linux for embedded development

Vim for the win

I came to a point where Vim serves me well as an IDE for all of my needs. It works extrememly well for a range of different development needs.

Be it Rust, Typescript, Vue.js, C, Python - developing in either of them is a joy in a nicely configured Vim editor. Most of the magick happens thanks to the Coc.nvim and corresponding Language Server extension.

C/C++ development in Vim

To set up a C/C++ project in Vim you need few pieces.

  • Vim - duh
  • Coc.vim - the language server client
  • CCLS - C/C++ language server
  • compile_commands.json - the missing link

compile_commands.json

This is the secret ingredient. This file is generated by the build system of your project and it contains the data about the include files locations and compile options that CCLS uses to generate autocomplesion and autosuggestions prompts. There is multiple ways to generate this file. Some are straigforward and some are not. One i used reliably is CMake, passing -DCMAKE_EXPORT_COMPILE_COMMANDS=YES into the command will generate the JSON file. There are however other methods of generating such file.

Final boss

Since i had to do a bit of work in the area of Stm32 development i started to appreciate Stm32CubeIDE code generation features. By selecting the required peripherals and their configuration the Stm32CubeIDE will kindly generate the initialization code for you. However you're then stuch with the Stm32CubeIDE enforced project structure and no compile_commands.json file. You can possibly retrofit the CMAKE into the project but i was not able to do this succesfully. Additionally Stm32CubeIDE ises Makefile by default and i didn't really botther getting too deep into an Eclipse plugin ecosystem. The idea of having to go back and work in Eclipse was bringing flash back of my early Java development days.

The solution

I came up with a setup that allows me to work on the project both ways: through IDE and Vim. For this to work we need to generate the compile_commands.json file somehow.

Vim

I will assume that your Vim is already configured with a plugin manager and Coc plugin. If not just follow the official guide from the Coc repository. You will also need the CCLS server running on your machine. Install it by typing:

sudo pacman -S ccls

NOTE: Do not install coc-ccls extension. Its a unmaintained extension and is not needed in this setup. CCLS works out of the box with Coc.

compiledb

The first step is to install compiledb which will allow us to generate the compile_commands.json. We can install it with pip:

python3 -m pip install --user --upgrade compiledb

Stm32CubeIDE

Now create new project as you would normally do it. Then Right click on project and select properties. Now edit the Builder settings as per the below image by adding the following as the build command:

compiledb ../compile_commands.json make -C Debug -j8

Now every time you build your project it will also generate the compile_commands.json file in the root of your project. One last step is to add .ccls file in the root of your project with following content:

%compile_commands.json

This is so that the ccls knows the exact path to all the files your project is using and where to look for stuff to autosuggest in your Vim.

Happy coding!