Linux cmake Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux cmake command and its practical applications. We will start by installing CMake, a popular build automation tool, on our Ubuntu 22.04 environment. Then, we will create a simple C++ project and learn how to use CMake to manage the build process. Finally, we will understand the different build configurations available in CMake.

The steps covered in this lab are essential for developers who work with C/C++ projects and need to manage complex build processes efficiently. By the end of this lab, you will have a solid understanding of how to use CMake to streamline your development workflow.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") subgraph Lab Skills linux/echo -.-> lab-422608{{"`Linux cmake Command with Practical Examples`"}} linux/apt -.-> lab-422608{{"`Linux cmake Command with Practical Examples`"}} linux/sudo -.-> lab-422608{{"`Linux cmake Command with Practical Examples`"}} end

Install CMake on Ubuntu 22.04

In this step, we will install CMake, a popular build automation tool, on our Ubuntu 22.04 environment.

First, let's update the package index:

sudo apt-get update

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done

Next, install the necessary packages for CMake:

sudo apt-get install -y cmake

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  cmake-data libarchive13 libjsoncpp24 librhash0 libssl-dev libssl1.1
Suggested packages:
  cmake-doc
The following NEW packages will be installed:
  cmake cmake-data libarchive13 libjsoncpp24 librhash0 libssl-dev libssl1.1
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 15.6 MB of archives.
After this operation, 67.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libssl1.1 amd64 1.1.1m-3ubuntu1 [1,296 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 libssl-dev amd64 1.1.1m-3ubuntu1 [1,547 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libarchive13 amd64 3.5.2-2 [262 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 libjsoncpp24 amd64 1.9.5-2 [104 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 librhash0 amd64 1.4.2-1 [106 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy/universe amd64 cmake-data all 3.22.1-1ubuntu1 [1,605 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy/universe amd64 cmake amd64 3.22.1-1ubuntu1 [10.7 MB]
Fetched 15.6 MB in 3s (5,204 kB/s)
Selecting previously unselected package libssl1.1:amd64.
(Reading database ... 78852 files and directories currently installed.)
Preparing to unpack .../libssl1.1_1.1.1m-3ubuntu1_amd64.deb ...
Unpacking libssl1.1:amd64 (1.1.1m-3ubuntu1) ...
Selecting previously unselected package libssl-dev:amd64.
Preparing to unpack .../libssl-dev_1.1.1m-3ubuntu1_amd64.deb ...
Unpacking libssl-dev:amd64 (1.1.1m-3ubuntu1) ...
Selecting previously unselected package libarchive13:amd64.
Preparing to unpack .../libarchive13_3.5.2-2_amd64.deb ...
Unpacking libarchive13:amd64 (3.5.2-2) ...
Selecting previously unselected package libjsoncpp24:amd64.
Preparing to unpack .../libjsoncpp24_1.9.5-2_amd64.deb ...
Unpacking libjsoncpp24:amd64 (1.9.5-2) ...
Selecting previously unselected package librhash0:amd64.
Preparing to unpack .../librhash0_1.4.2-1_amd64.deb ...
Unpacking librhash0:amd64 (1.4.2-1) ...
Selecting previously unselected package cmake-data.
Preparing to unpack .../cmake-data_3.22.1-1ubuntu1_all.deb ...
Unpacking cmake-data (3.22.1-1ubuntu1) ...
Selecting previously unselected package cmake.
Preparing to unpack .../cmake_3.22.1-1ubuntu1_amd64.deb ...
Unpacking cmake (3.22.1-1ubuntu1) ...
Setting up libssl1.1:amd64 (1.1.1m-3ubuntu1) ...
Setting up libssl-dev:amd64 (1.1.1m-3ubuntu1) ...
Setting up libarchive13:amd64 (3.5.2-2) ...
Setting up libjsoncpp24:amd64 (1.9.5-2) ...
Setting up librhash0:amd64 (1.4.2-1) ...
Setting up cmake-data (3.22.1-1ubuntu1) ...
Setting up cmake (3.22.1-1ubuntu1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3) ...

The installation is now complete. You can verify the CMake version by running:

cmake --version

Example output:

cmake version 3.22.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Great! We have successfully installed CMake on our Ubuntu 22.04 environment.

Create a Simple C++ Project with CMake

In this step, we will create a simple C++ project and use CMake to build it.

First, let's create a new directory for our project:

cd ~/project
mkdir cpp-project && cd cpp-project

Next, create a new C++ file named main.cpp with the following content:

#include <iostream>

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

Now, we need to create a CMakeLists.txt file in the same directory to define our project's build process. Add the following content to the file:

cmake_minimum_required(VERSION 3.22)
project(cpp-project)

add_executable(cpp-project main.cpp)

This CMake file sets the minimum required CMake version, defines the project name, and adds an executable target named cpp-project that will be built from the main.cpp file.

To build the project, run the following commands:

cmake -S . -B build
cmake --build build

The first command generates the build files in the build directory, and the second command actually builds the project.

Example output:

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/labex/project/cpp-project/build
Scanning dependencies of target cpp-project
[ 50%] Building CXX object CMakeFiles/cpp-project.dir/main.cpp.o
[100%] Linking CXX executable cpp-project
[100%] Built target cpp-project

Finally, run the built executable:

./build/cpp-project

Example output:

Hello, CMake!

Congratulations! You have successfully created a simple C++ project and built it using CMake.

Understand CMake Build Configurations

In this step, we will explore the different build configurations available in CMake and how to use them.

CMake supports several build configurations, such as Debug, Release, RelWithDebInfo, and MinSizeRel. These configurations determine the optimization and debugging settings used during the build process.

Let's start by creating a new C++ project:

cd ~/project
mkdir cmake-configurations && cd cmake-configurations

Now, create a main.cpp file with the following content:

#include <iostream>

int main() {
    std::cout << "Build Configuration: " << CMAKE_BUILD_TYPE << std::endl;
    return 0;
}

Next, create a CMakeLists.txt file with the following content:

cmake_minimum_required(VERSION 3.22)
project(cmake-configurations)

add_executable(cmake-configurations main.cpp)

To build the project with the default Debug configuration, run the following commands:

cmake -S . -B build
cmake --build build

Example output:

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/labex/project/cmake-configurations/build
Scanning dependencies of target cmake-configurations
[ 50%] Building CXX object CMakeFiles/cmake-configurations.dir/main.cpp.o
[100%] Linking CXX executable cmake-configurations
[100%] Built target cmake-configurations

Run the built executable:

./build/cmake-configurations

Example output:

Build Configuration: Debug

Now, let's build the project with the Release configuration:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

Example output:

-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/labex/project/cmake-configurations/build
Scanning dependencies of target cmake-configurations
[ 50%] Building CXX object CMakeFiles/cmake-configurations.dir/main.cpp.o
[100%] Linking CXX executable cmake-configurations
[100%] Built target cmake-configurations

Run the built executable:

./build/cmake-configurations

Example output:

Build Configuration: Release

As you can see, the build configuration is reflected in the output of the executable. The Debug configuration is used by default, but you can specify a different configuration using the -DCMAKE_BUILD_TYPE option when running cmake.

Summary

In this lab, we first installed CMake, a popular build automation tool, on our Ubuntu 22.04 environment. We updated the package index and then installed the necessary packages for CMake. Next, we created a simple C++ project and learned how to use CMake to build and manage the project. We also explored different CMake build configurations, which allow us to customize the build process for different environments or purposes.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like