Total Pageviews

Monday, 13 June 2016


CMake, the cross-platform, open-source build system. 


CMake is a cross-platform, open-source build system generator. For full documentation visit the CMake Home Page and theCMake Documentation Page.
CMake is maintained and supported by Kitware and developed in collaboration with a productive community of contributors.


CMake is distributed under the OSI-approved BSD 3-clause License. See Copyright.txt for details.

Building CMake

Supported Platforms

MS Windows, Mac OS X, Linux, FreeBSD, Solaris, HP-UX, IRIX, BeOS, QNX
Other UNIX-like operating systems may work too out of the box, if not it should not be a major problem to port CMake to this platform. Subscribe and post to the CMake Users List to ask if others have had experience with the platform.

Building CMake from Scratch


You need to have a compiler and a make installed. Run the bootstrap script you find in the source directory of CMake. You can use the --help option to see the supported options. You may use the --prefix=<install_prefix> option to specify a custom installation directory for CMake. You can run the bootstrap script from within the CMake source directory or any other build directory of your choice. Once this has finished successfully, run make and make install. In summary:
$ ./bootstrap && make && make install


You need to download and install a binary release of CMake in order to build CMake. You can get these releases from the CMake Download Page . Then proceed with the instructions below.

Building CMake with CMake

You can build CMake as any other project with a CMake-based build system: run the installed CMake on the sources of this CMake with your preferred options and generators. Then build it and install it. For instructions how to do this, see documentation on Running CMake.

Reporting Bugs

If you have found a bug:

  1. If you have a patch, please read the CONTRIBUTING.rst document.
  2. Otherwise, please join the CMake Users List and ask about the expected and observed behaviors to determine if it is really a bug.
  3. Finally, if the issue is not resolved by the above steps, open an entry in the CMake Issue Tracker.

CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.
CMake is part of Kitware’s collection of commercially supported open-source platforms for software development.



CMake is a collection of building, testing and packaging tools. Admittedly autoconf/automake is the de facto tools for opensource project in a long period of time, there are many reasons for chosing CMake: it is powerfull and easy to use, supports various IDE, and cross platform.
In this article I write down various snippets during my daily usage of CMake/CTest/CPack. In addtion, I assumed that the readers are already familiar with CMake, otherwise Mastering CMake and the official online doc are the best material for reference.


  1. Generate both static and shared library file
    add_library(foo SHARED SRCLIST)addlibrary(foostaticSTATIC{SRC_LIST}) set_target_properties(foo_static PROPERTIES OUTPUT_NAME “foo”)
  2. Use different compiler
    The CMAKE_C_COMPILER and CMAKE_CXX_COMPILER are used for these purpose, but those variable must be set before project() command. The recommended way is using “cmake -D”, i.e.: cmake -D CMAKE_CXX_COMPILER=/path/to/your/compiler
  3. Retrieve the absolute path
    There’re three frequently-used variables:
    • CMAKE_SOURCE_DIR The full path to the top level of source tree
    • CMAKE_CURRENT_SOURCE_DIR The full path to the source directory currently being processed
    • CMAKE_CURRENT_BINARY_DIR The full path to the build directory that is currently being processed
  4. Pass value to parent directory.
    Certainly it is not a good practice to pass value to parent directory, sometimes it’s reasonable to do so. Here are three methods:
    • use PARENT_SCOPE with set(the simplest method) in subdir: set(VAR_IN_SUB “put something here” PARENT_SCOPE)
    • set variable in sub directory, and then get its definition in subdir: set(VAR_IN_SUB “put something here”) in current dir: get_directory_property(VAR DIRECTORY subdir DEFINITION VAR_IN_SUB)
    • set directory property in sub directory, and then get its value in subdir: set_directory_properties(PROPERTIES PRO_IN_SUB “put something here” in current dir: get_directory_property(VAR DIRECTORY subdir PRO_IN_SUB)
  5. Generate version, build time/host etc. info.