CMake is an open-source build system generator that helps manage the build process of projects across different platforms. It is not an IDE but generates native build files for various platforms, which can be used with IDEs or other build tools. It simplifies the process of building complex projects across different environments. This is done by specifying the build configuration in the projects to help developers generate native build files for various platforms.
CMake provides the basic tools: cmake
, ctest
, cpack
, cmake-gui
, and ccmake
. In this Answer, we’ll discuss the working of these five and how they assist in building CMake projects.
cmake
executableThe cmake
command has multiple sets of operations it can perform, such as the following:
Generating project build system
The cmake
command is used to configure and generate the build system. To build the project, you can use the following command:
cmake --build <dir>
To build the project with arguments specific to our project, we can specify it in the build command using:
cmake --build <dir> [<options>] [-- <build-tool-options>]
Building, installing, and running a project
The cmake
command can also be used to run already configured projects by installing it. We can continue this by moving the files into the right directories, installing the dependencies, and running the custom installation command like:
cmake --install <dir> [<options>]
Running a script or command-line tool
As a special function of cmake
, we can run commands in a platform-independent way. We might need to copy a file, run a script, etc. To do this, cmake
offers an option to execute the common commands in the same way across many platforms. For this, we use the following command:
cmake -E <command> [<options>]
ctest
executableTo optimize and maintain high-quality codes, we use automated code testing. It can be an important step during the build process. ctest
helps standardize running tests and report the results for projects built with CMake. It integrates with CMake to ensure that tests are run and results are reported consistently. This means total transparency of the testing process, including the framework used and the method of running it. If, after testing, there is a requirement to rebuild the project, ctest
can also trigger build using cmake
. We can call tests for a build project using the following command:
$ ctestTest project C:/Users/educative/CMake/buildGuessing configuration DebugStart 1: SystemInformationNew1/1 Test #1: SystemInformationNew ......... Passed 3.19 sec100% tests passed, 0 tests failed out of 1Total Test time (real) = 3.24 sec
cpack
executableNow that projects are built and tested, they are ready to be launched for public access. Some users may use the project with the already precompiled binaries. However, some may require different binaries and dependencies. cpack
here can be used to create custom packages for different platforms. The types of installers it provides are:
Compressed archives
Executable installers
Wizards and RPMs
NuGet and DMG packages
cpack
works in the same way cmake
works. It uses a set of installing dependencies, generating packages, and using generators to pick the specific binaries needed for the build.
cmake-gui
executableCMake also provides a graphical user interface for users who aren’t familiar with command line processes. CMake provides the cmake-gui
package for Windows and for Unix-like platforms, there is a specific QT package to run it.
It is important to note that GUI provides limited execution options. To access the CMake GUI, run the executable using:
cmake-gui
Upon execution of the command, the users will see the following:
ccmake
executableThe ccmake
executable is available in Unix-like platforms (not available for Windows). ccmake
is a CMake curses interface. It is not available as part of the package. To install the ccmake
interface in your system, run the following command:
sudo apt-get install cmake-curses-gui
We can include the specifications of the project we want to run by using the following command:
ccmake [<options>]ccmake {<path-to-source> | <path-to-existing-build>}
The ccmake
executable is the same as CMake GUI:
Like CMake GUI, it also has limited options. We have now covered all the tools available in CMake, their execution, installation, and configuration on multiple platforms.
What are the basic tools in CMake?
What is the primary purpose of CMake?
To serve as an Integrated Development Environment (IDE)
To compile code directly
To generate native build files for various platforms
To automate software testing
CMake provides tools for various tasks, which include generating build systems, building projects, running scripts, and accessing command-line and GUI applications. ctest
and cpack
enhance the development process by standardizing testing and packaging. This ensures that projects are reliable and easy to distribute.
Free Resources