When building something I usually either let an IDE handle the build process, or write makefiles manually. Both solution work, but have drawbacks. IDEs allow you to forget about how code is built, but create some sort of IDE lock-in, since switching to another IDE becomes difficult. Makefiles solve this problem, but are a bit too low level, and writing makefiles for complex projects is tedious.
So it’s a while I’ve been searching a better way. The most known alternative is autoconf and automake. If you’ve compiled some software on Linux you’ll be familiar with the
./configure <options> make make install
way of doing things. However, these tools have the main problems of being built mostly with shell scripts. If you plan to support only Linux this is not an issue, but it becomes a bigger trouble if your program needs to be compiled on windows too, where bash is not available by default.
Recently I’ve found CMake, and it looks interesting. First it’s the tool used to build KDE4, and this means it’s well suited even for large projects, second it claims to work equally well on Linux, Mac and Windows. And lastly, its syntax seems simple and high level.
I’ve started with the documentaton on its website, and in 15 minutes I’ve been able to compile an example program. The example program is made of three files, main.cpp, foo.cpp and foo.h.
The first step to create a CMake project is to write a file named CMakeLists.txt. Its content is this:
cmake_minimum_required (VERSION 2.6) project (HELLO) add_executable (hello main.cpp foo.cpp)
The first line is a requirement for a new version of CMake. The second specifies the name of the project, and the third tells CMake to create an executable named hello by compiling the files main.cpp and foo.cpp. Really simple.
Like autoconf and automake, CMake doesn’t compile the code directly, instead it generates a makefile. The steps to compile the example program are to open a shell in the directory where the source files and CMakeLists.txt are, and type:
mkdir build cd build cmake ../ make
This is because with CMake it is recomended to keep the build directory separated from the source directory. To remove the generated files it is either possible to type “make clean” or simply to delete the build directory.
It is still early to say if I’ll use CMake for my projects, after all I’ve just started using it for 15 minutes, but it surely looks interesting.