Lately I have had to get into c++ programming in order to do some live analysis of our MiniBees. We also want to start working with live visuals in our performances as well, so last year I got a hold of a second-hand Kinect, and the last couple of weeks I’ve been playing around with this in oF, a c++ version of the excellent java-based graphics programming environment Processing.
I have spent a lot of time tweaking my preferred coding environment, which is based on the text editor neovim, a modern fork of vim, so I wanted to keep using this for my c++ coding as well. Turns out this is not too hard!
Installing openFrameworks on Arch Linux
In order to get oF to work in Arch Linux you need to follow this guide. It’s all pretty straightforward: download the repository to your computer and put it in a reasonable location, run a couple of scripts, and off you go! The first script you need to run is
$OF_ROOT/scripts/linux/archlinux/install_dependencies.sh. This is a wrapper script for
pacman that will download and install all the necessary dependencies. After this you probably want to run
$OF_ROOT/scripts/linux/compileOF.sh. This will compile all the libraries you need. Finally, you probably want to use the
projectGenerator to create new projects, so run
$OF_ROOT/scripts/linux/compilePG.sh as well.
The oF developers do not officially support the AUR package. Sticking with the official method of installation makes it easier to juggle different versions of oF later on.
Set up Neovim
In order to get an ergonomic environment set up, I use a couple of plugins:
vim-gutentags. I don’t use YouCompleteMe or any such thing, as I find nvims omnicomplete function sufficient for my needs. Use your favorite plugin manager to install these. I use
Plug 'ludovicchabant/vim-gutentags' Plug 'vim-scripts/a.vim'
To get autocompletion (somehow) working for oF, you need to tweak gutentags a little bit. The plugin uses some default markers (the presence of .git, for example) to define the root of a project. Setting up the entire oF tree as one git project would be total overkill and defeat the whole purpose of this kind of framework, so it is easier and better to disable gutentags’ defaults and add your own. In
$XDG_CONFIG_HOME/nvim/init.vim add these lines:
let g:gutentags_project_root = ['.gutctags'] let g:gutentags_add_default_project_roots = 0
Next, add the
.gutctags file to the oF root folder:
$ touch $OF_ROOT/.gutctags
Now, whenever you do anything in your oF tree, gutentags will quietly fill up a tag file with anything you need for nvim’s omnicomplete to work as expected.
The one downside to this approach is that your tag file does indeed fill up with EVERYTHING, so every little typo you made or other apps you wrote in the same oF tree become food for the tag generator. I don’t have a good solution for this yet.
Make Make Work
Coming from an interpreted language like SuperCollider to a compiled language like c++ is a bit painful, but with a couple of tweaks to nvim the transition can be smoother. This is where nvim’s excellent
terminal capabilities enter into the picture. Add these lines in your
augroup c autocmd! autocmd FileType c,cpp,h,hpp,glsl call MakeRun() augroup end function! MakeRun() nnoremap <C-e> :terminal make -j8 && make run<cr> inoremap <C-e> <esc>:terminal make -j8 && make run<cr> endfunction
This will make nvim behave in a similar way to the
scnvim plugin that I use for SuperCollide development. Whenever I’m in a source file I can hit ctrl-E, and a terminal window will open up, compile the project and then run it. Very sweet indeed.
Navigating between Headers and Implementation Files
One of the pains of c++ is that whole header/implementation file splitup. I’m sure there are very good reasons for it, but it’s just a bit frustrating sometimes. To ease the pain I’m using
a.vim and mapping it to a keyboard sequence. In
init.vim this becomes:
" new split with alternate file nnoremap mv :AV<cr> " switch in same window nnoremap ma :A<cr>
So, in normal mode, if I am working on
ofApp.h and want
ofApp.cpp in a vertical split, I hit
mv. If I just want to switch in the current view, I hit
ma. Very handy.