How to Build Linux Server Programs with CMake

We have previously illustrated how to compile a program with Make. In this brief guide, you will learn a little bit about building with CMake, a useful alternative found in many Linux distributions.

First, you should install CMake on your system, if it is not already present. On RHEL and CentOS, type:

# yum install cmake

On Debian/Ubuntu type:

# apt-get install cmake

Next, install any dependencies required by the program. There should be a list of them in the README or INSTALL file. If not, you might need to check the program’s documentation.

Next, you should navigate to the directory containing the cmake files and create a build directory, and then change to that directory:

$ mkdir build

$ cd build

Then, simply run CMake:

$ cmake ..

You can consult the CMake documentation for available options. For example, to select a build type, you would pass an option like this:

cmake .. -DCMAKE_BUILD_TYPE=Release

Once CMake has generated the buildscripts, you should run Make as you normally would.

$ make

And finally, install the program:

# make install

For more information about CMake as well as full developer information, visit the CMake resources website.