Install and Use CMake on Ubuntu 22.04

In this guide from the Linux Tutorials, we want to teach you to Install and Use CMake on Ubuntu 22.04.

CMake is an open-source, cross-platform, and free tool (or meta-build system), that some would describe as a build system generator that is used in conjunction with your build environment.

It is utilized to control the build process (perform build configuration) by using configuration files or what you would call scripts (CMakeLists.txt) to generate build files. In addition to supporting various build systems and operating systems, it also supports several programming languages such as C++, C#, Cuda, and more.

Install and Use CMake on Ubuntu 22.04

To install CMake, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.

Now follow the steps below to complete this guide.

Installing CMake on Ubuntu 22.04 From Source

First, update your local package index with the following command:

sudo apt update

Next, install the required packages and dependencies on Ubuntu 22.04:

sudo apt install build-essential checkinstall zlib1g-dev libssl-dev -y

To install the latest CMake version, you need to visit the CMake downloads page.

Then, use the wget command to download the latest stable version of CMake:

wget https://github.com/Kitware/CMake/releases/download/v3.23.2/cmake-3.23.2.tar.gz

Extract your downloaded file:

tar -zxvf cmake-3.23.2.tar.gz

Next, switch to your CMake directory on Ubuntu 22.04:

cd cmake-3.23.2

At this point, use the following commands to install CMake:

./bootstrap

The Bootstrap script may take a few minutes.

Use the make or gmake command to build your process:

# make 
or
# gmake

Now install CMake using the following:

sudo make install

This process can take a few minutes to almost ten minutes.

Verify your CMake installation on Ubuntu 22.04 by checking its version:

cmake --version
Output
cmake version 3.23.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).

When your installation is completed, let’s see how to use the CMake.

How To Use CMake

At this point, we want to teach you how to use CMake by creating a sample project hello world on Ubuntu 22.04.

First, you need to create a directory for your project and switch to it with the following command:

# mkdir cmproject
# cd cmproject

Then, you need to create a C++ source file with your favorite text editor, here we use vi:

vi hello.cpp

Add the following content to the file:

#include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0 ; }
When you are done, save and close the file.
Next, you need to create the CMakeLists.txt file (with this exact capitalization) which is required by CMake:
vi CMakeLists.txt

The root directory of the project ( ~/cmproject in this case) must contain a CMakeLists.txt file. Each line in a CMakeLists.txt file has a command.

The CMakeLists.txt file for this project is quite trivial and will only contain these three lines:

cmake_minimum_required(VERSION 3.18) project(MyProject VERSION 0.0.1) add_executable(hello hello_world.cpp)

When you are done, save and close the file.

It’s recommended to have a separate build directory for executables. To do this, run the following command:

mkdir build
The project structure looks like this now:
[[email protected] cmproject]# tree
.
├── build
├── CMakeLists.txt
└── hello.cpp

1 directory, 2 files
To run CMake, you need to switch to your build directory on Ubuntu 22.04:
cd build
Run the CMake command:
cmake ..
Note: The .. is an alias for the parent directory and tells cmake to find the CMakeLists.txt file in the parent directory. If you see CXX anywhere while working with CMake, it refers to C++ as CXX.
Now you can generate the executable simply by typing:
cmake --build .
Then, run the executable file:
./hello
Output
Hello World!

Conclusion

At this point, you learn to Install and Use CMake on Ubuntu 22.04.

Hope you enjoy it.

You may be interested in this article How To Install CMake on Ubuntu 20.04.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular