CMake is a free multi-language multi-platform compilation tool (the name stands for cross-platform make). Although Autotools is the traditional tool used on Linux - used by the GNU project among others -, various projects have changed to CMake during the last few years, for different reasons. Important examples are KDE and MySQL. Those who have already been rubbed by Autotools to construct their own project will probably find CMake a lot more easy to use. In fact, according to KDE, the main reasons for which they have changed from Autotools to CMake is that the compilation is a lot quicker and that the build files are a lot easier to write.
CMake works in the same way as that Autotools requires to run a configure script, followed by a build with make. However, instead of calling ./configure, you call cmake directory. For example, if you are inside the directory where you would like to build the application, you run
[name@server $] cmake .
Hence, to configure, build and install an application or a library, the simplest way to do this is
[name@server $] cmake . && make && make install
Customizing the configuration
Just like with autotools, it is possible to customize the configuration of an application or a library. This can be done by different command-line options, but also using a command line interface, using the ccmake command.
You call ccmake in the same way as you call cmake: by giving the directory to build from. So if this is the current directory, you should call:
[name@server $] ccmake .
You should run ccmake after having ran cmake. So, generally you would do
[name@server $] cmake . && ccmake .
ccmake gives you then a list of options that are defined by the project. You will then see a relatively short list like this:
[name@server $] cmake . && cmake .. Page 1 of 1 ARPACK_LIBRARIES ARPACK_LIBRARIES-NOTFOUND CMAKE_BUILD_TYPE CMAKE_INSTALL_PREFIX /usr/local CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk GSL_CONFIG /opt/local/bin/gsl-config GSL_CONFIG_PREFER_PATH /bin;;/bin; GSL_EXE_LINKER_FLAGS -Wl,-rpath,/opt/local/lib NON_TEMPLATES_DISABLED ON NO_SQUACK_WARNINGS ON PRECOMPILED_TEMPLATES ON USE_GSL_OMP OFF USE_OMP OFF Press [enter] to edit option CMake Version 2.8.8 Press [c] to configure Press [h] for help Press [q] to quit without generating Press [t] to toggle advanced mode (Currently Off)
As is written at the bottom of this display, you can edit a value by pressing the enter key. If you modify a value, you will want to press the c key to try out the configuration with this new value. If this new configuration succeeds with that new value, you will then have the option g to generate the Makefile with the new configuration, or you can quit using the q key. Lastly, you can activate advanced mode using the t key. You will then have a much longer list of variables which allows you to precisely configure the application. Here is a list of options
As you can see, ccmake in advanced mode displays equally well the libraries that were found as those that were not found. If you would like to use a specific version of BLAS for example, you will immediately know which one was found by CMake, and modify this if necessary. ccmake also displays the list of flags that are passed to the C, C++, and other compilers, to the linker, depending on the build type.
Command line options
All command line options that are displayed by ccmake can be modified on the command line, using the following syntax:
[name@server $] cmake . -DVARIABLE=VALUE
For example, to specify the install location:
[name@server $] cmake . -DCMAKE_INSTALL_PREFIX=/home/user/my_directory
To configure the compilation, you might want to change the following values:
|CMAKE_C_COMPILER||Change the C compiler|
|CMAKE_CXX_COMPILER||Change the C++ compiler|
|CMAKE_LINKER||Change the linker|
|CMAKE_C_FLAGS||Change the flags passed to the C compiler|
|CMAKE_CXX_FLAGS||Change the flags passed to the C++ compiler|
|CMAKE_SHARED_LINKER_FLAGS||Change the flags passed to the linker|
A more exhaustive list option is available on the official CMake page.
If you do not want to get into adventures with these specific options, CMake also provides a simpler option, called CMAKE_BUILD_TYPE. This option defines which compilation type must be used. Possible values are
|Debug||Activate debugging options, deactivate optimization options|
|Release||Deactivate debugging options, activate usual optimizations|
|MinSizeRel||Deactivate debugging options, activate optimization options that minimize the binary's size|
|RelWithDebInfo||Activate debugging options and usual optimizations|
These different compilation types define compiler options that vary from compiler to compiler. So you do not need to check which exact compiler flags have to be used.