Almost all projects that I’m working on are using CMake today with out-of-source builds, which means it doesn’t pollute your source directory with generated files. Normally this means I create “bld” folder on the top-level project directory and run CMake to generate the Makefile like this:

cd /path/to/project
mkdir bld
cmake ../src

When using an IDE like KDevelop or QtCreator you have a project which handles CMake and building is as is simple as pressing F7. But often for testing or some quick hacks I don’t fire up an IDE, I just use vim to edit the files and also have mapped F7 to run “:make” inside vim. Unfortunately when you are not in the build directory you cannot run make inside vim or on the konsole. You first need to change into the build directory run make and go back to directory where you came from. This is tedious so I created the little script ‘mk’ which I put into /usr/local/bin. This script finds the build directory automatically and runs make for me in there. So where every you are in the source tree instead of

cd ../../../bld
cd -

you just type


and you are done. In vim you can use “:set makeprg=mk” to use mk instead of make for building.

How does this work? I’m using git to manage all my projects. This script uses ‘git rev-parse –show-toplevel’ to find the git top-level directory. From there I add by convention the name ‘bld’ for my build directory. The script also detects if you are inside a submodule so this still works. For all other cases you still have the option to export the env variable MK_BLDIR to define where your build directory is.

Have a look at the script for more details:


comments powered by Disqus