Linux Commands

How to Use Cmake for Linux

CMake is a tool that is used for the automation of the process of building, testing, and packaging software. It is an open-source, cross-platform build system generator that allows the developers to manage the build process of software projects by generating makefiles or other build scripts for a wide range of build systems such as Unix Makefiles, Ninja, and Microsoft Visual Studio.

In simpler terms, when you are writing a software program, you need to build it to make it work on your computer. This process involves compiling the code and linking it to any required libraries, creating executable files, and other tasks. CMake is a tool that helps automate this process by creating scripts that define how your program should be built.

These scripts are saved in a file called “CMakeLists.txt” which contains a list of commands that tell CMake where to find the source code for your program, what libraries it depends on, and how to compile and link everything together. By reading and interpreting this file, CMake can generate the necessary files for building your program on different platforms such as Unix Makefiles or Visual Studio projects.

In other words, CMake makes it easier to build your program by creating a standardized way of defining the build process, regardless of the specific build system you are using. This can save you a lot of time and effort when working on complex projects that require building on multiple platforms or with different compilers.


Before we can start using CMake, we need to install it on our Linux system. CMake is available in the package repositories of most Linux distributions, so the installation is a simple matter of using the package manager.

Step 1: Install CMake with the following command:

$sudo apt-get install cmake

You will see a similar output to the one in the following terminal:

Step 2: Verify the installation by running the following command:

$cmake --version

This should output the version of CMake that is installed on the system.

User Guide

To demonstrate how to use CMake, let’s create a simple C++ project that consists of a single source file. For the purposes of this guide, we will make a simple script that outputs the “Hello, CMake” string to the console. We will use CMake to generate a makefile for the project which we can then use to build the project.

Step 1: First, create a new directory for the project and navigate to it. Then, create a new file called “main.cpp” with the following contents:

#include <iostream>

int main() {
std::cout << "Hello, CMake!" << std::endl;
return 0;

This is a simple “Hello, CMake!” program that outputs a message to the console.

Step 2: Create a new file called “CMakeLists.txt” in the project directory with the following contents:

cmake_minimum_required(VERSION 3.10)


add_executable(hello main.cpp)

Note: This “CMakeLists.txt” file contains the instructions for CMake to generate a makefile for our project. The “cmake_minimum_required” command specifies the minimum version of CMake that is required to build the project, and the project command sets the name of the project. The “add_executable” command specifies the name of the executable that we want to build (hello) and the source files that should be compiled (main.cpp).

Step 3: Generate the makefile for the project by running the following command in the project directory:

$cmake .

This generates a makefile in the current directory that we can use to build the project.

Step 4: Build the project by running the following command:


This compiles the source code and creates an executable called “hello” in the project directory.

Step 5: Run the executable by running the following command:


This should output the “Hello, CMake!” message to the console.


We covered the basics of using CMake for Linux. We learned how to install CMake on a Linux system and how to create a simple C++ project using CMake. CMake is a powerful tool that can be used to manage even the most complex software projects, and we have only scratched the surface of its capabilities. It simplifies the build process for software development projects and provides a standardized and platform-independent way of defining the build process which makes it easier for developers to manage their projects and build them on different platforms. With CMake, you can automate the process of building, testing, and packaging their software which saves them time and effort. Although CMake may come with a steep learning curve, it is worth the investment in time and effort, especially for large and complex projects. The flexibility and portability of CMake make it a valuable tool for developers, and it has become a widely adopted standard in the software development industry.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.