Using vim for c++; Code completion and syntax highlighting (Ubuntu 22.04)
Overview
I’m currently using neovim as my IDE for writing a c++ library. The end goal is to have a common c++ library that I can load into multiple other languages — Python and R — where I would then write simple wrapper packages for it. Using minGW, I can even use cross-compile the library for windows and provide an installer exe written in NSIS. Windows users will only have to double click the installer, to install it.
My code is pretty simple so I don’t need a particularly complex IDE. I just want something to highlight errors, give me code completion, and show definitions functions. The vim plugin YouCompleteMe (YCM) has been working great for this!
However, if you’ re relatively new to c++ (like me) and don’t have a solid grasp on the c++ compiler ecosystem, the setup is a bit tricky. Here, I’m just giving a walk-through of what I had to do to get this working. After the setup of YCM, vim serves as a really great IDE for c/c++.
The steps are rather simple once you’re familiar with all the parts:
- Install YouCompleteMe
- Setup the compilation database for clangd
- Generate ctags
- Install missing headers for standard c++ libraries.
Install YCM
First, install YCM . Note that there is an extra step after the installation and you’ll need to run a python script to finish the setup. From the docs:
cd ~/.vim/bundle/YouCompleteMe
python3 install.py --all
Create compiler database
YCM uses clangd for code completion and highlighting syntax errors. Here, clangd (and clang) requires a JSON database to be initialised beforehand. As far as I can tell, this more-less just gives a platform independent summary of the file dependencies listed in your makefile recipes. I used bear to create this.
sudo apt install bear
After installing bear
, run your build command within it and the compiler database will be created.
#!/bin/bash
# clean build directory
make distclean
# Create compialtion database
./configure
bear -- make
Setup ctags
YCM also makes use of ctags. This lets you quickly jump between spots in your code. For example, if your cursor is hovering over a function, you can use ctags to immediately jump to where that function was declared or defined. YCM specifically uses exuberant-ctags.
sudo apt install exuberant-ctags
You can recursively build your tag database with the following command:
ctags -R --fields=+l
Note that you’ll have to manually run this to rebuild your tags. This is the simplest approach, but there are vim plugins that will automate this for you and keep your tags up-to-date. I haven’t had time to try out these plug-ins yet, which is why I’m leaving them out here.
Get developer versions of standard c++ libraries
When opening my c++ files, all the include statements for the standard libraries — such as #include <vector>
— were giving file not found
errors while my code compiled just fine. The issues was that I was missing the appropriate developer versions of these libraries used by clang. The fix is simple: (1) determine what GCC version clang is using, then (2) install the dev libraries for that particular version.
Invoke clang (what YCM uses) with the verbose tag. In case its not installed on your system, simply sudo apt install clang
beforehand.
$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
...
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
...
Here, we see its using version 12 of c++. Next, just install the dev version of those standard libraries.
sudo apt install libstdc++-12-dev
And that’s it. When you reopen vim, the errors for including standard libraries should be gone.
Parting thoughts
I’m a fan of using vim for c++. The library I’m writing is relatively small, but I’ve never felt like I’ve been missing anything or that vim was holding me back. I think the most appealing feature is how easy vim is to setup from scratch. I just install it on a new machine, get my init.vim
file, ask VimPlug to install my listed packages, and off i go. The c++ requirements add some small steps to that setup workflow… but overall, its very simple and fast.