Vim and CMake Out-of-Source Builds

March 30, 2010

If you use Vim you probably know you can use the make command to build your project. The make command looks for a Makefile in your current directory. If you are editing a file that is not in the current directory (meaning you use some Vim magic to change to the directory containing your edit buffer), make will start below the top-level of your build directory. This is often the case when doing an out-of-source build with CMake.

To get around this, you can use the makeprg command:

    :set makeprg=make\ -C\ ~/development/qgis/build

The -C switch tells make to change to development/qgis/build in your HOME directory. Once makeprg is set, start the build using:


As errors are encountered they are added to the Quickfix window. This allows you to easily navigate to the problem lines and make corrections, all within Vim.

If you want the build to continue even if it encounters errors, add the -k switch to the makeprg command. Remember to escape all spaces in the makeprg command with a backslash.

If you aren’t familar with Quickfix, get started by checking out the following in the Vim help system:

    :h quickfix
    :h cope
    :h ccl
    :h cn

Caveat: This requires GNU make (gmake) and is known to work on Linux and OS X. Your mileage on Windows may vary depending on your compiler…