Using CMake with vim
April 13th, 2008
I’ve recently moved from make to CMake which is a cross-platform build system. It’s quite nice if you want to be able to build for mac/win with the same makefile(CMakeLists.txt for CMake). The CMakeLists syntax is quite powerful and I haven’t regretted moving since.
One of the main ideas of CMake is out-of-source build - that is you use cmake like this
where directory contains the CMakeLists.txt and it builds everything in the current directory you are in. This is mostly because CMake is quite messy - it uses lots of files to build.
I use Vim for my development and its awesome ‘make’ compatibility which allows to build programs using make inside vim and view errors with special vim commands. Now as CMake uses unix makefiles on mac, it works quite well from vim. But then you’ll have to first generate makefiles with cmake inside the source directory for that to function. Not a nice idea really…
My solution to this is to use Makeobj. It is a script used to build using normal make outside of source directory. It uses OBJ_SUBDIR environment variable which defines the name of subdirectory where the build will take place.
How can it be used with CMake and vim? Here is the maker script I use
if [ -e makefile ]; then
make
else
export OBJ_SUBDIR=build
if [ -e CMakeLists.txt ]; then
if [ -d build ]; then
cd build
rm CMakeCache.txt
else
mkdir build
cd build
fi
cmake ../
cd ..
makeobj $1 $2 $3 $4 $5
fi
fi
Now in vim you just go



