Easy Steps Install and Use CMake on AlmaLinux 8

This tutorial intends to teach you How To Install and Use CMake on AlmaLinux 8. Let’s see what exactly CMake means.

CMake is known as a meta-build system. It doesn’t build your source code: instead, it generates native project files for the target platform. For example, CMake on Windows will solve Visual Studio; CMake on Linux will produce a Makefile; CMake on macOS will produce a project for XCode, and so on. That’s what the word meta stands for: CMake builds build systems.

A project based on CMake always contains the CMakeLists.txt file. This special text file describes how the project is structured, the list of source files to compile, what CMake should generate out of it, and so on. CMake will read the instructions in it and will produce the desired output. This is done by the so-called generators, CMake components responsible for creating the build system files.

Another nice CMake feature is the so-called out-of-source build. Any file required for the final build, executables included, will be stored in a separate build directory (usually called build/). This prevents cluttering up the source directory and makes it easy to start over again: just remove the build directory and you are done.

Now proceed to the following steps provided by the Orcacore team to Install and Use CMake on AlmaLinux 8.

Steps To Install and Use CMake on AlmaLinux 8

To Complete this guide, you must 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 AlmaLinux 8.

Now follow the steps below to Install and Use CMake on AlmaLinux 8.

Download and Install CMake on AlmaLinux 8

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

sudo dnf update -y

Then, install the required packages on your server with the following command:

sudo dnf install make automake gcc gcc-c++ kernel-devel openssl-devel

To Install and Use CMake on AlmaLinux 8, you need to visit the CMake downloads page and get the latest tar.gz file. Download the file by using the wget command as shown below:

sudo wget https://github.com/Kitware/CMake/releases/download/v3.24.0/cmake-3.24.0.tar.gz

Extract your downloaded file with the following command:

sudo tar -zxvf cmake-3.24.0.tar.gz

Switch to your CMake directory on AlmaLinux 8:

cd cmake-3.24.0

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

# ./bootstrap
# gmake
# sudo make install

These commands will take some time to complete. When you are finished, you can verify your CMake installation by checking its version:

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

Now that you have CMake installed on your server, let’s create your first project.

Create a Sample Project with CMake

At this step of Install and Use CMake on AlmaLinux 8, we want to teach you how to use CMake by creating a sample project Hello World on AlmaLinux 8.

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.13)  # CMake version check
project(MyProject)               # Create project "MyProject"
set(CMAKE_CXX_STANDARD 14)            # Enable c++14 standard
add_executable(hello hello.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:

[daniel@ cmproject]# tree
.
├── build
├── CMakeLists.txt
└── hello.cpp

1 directory, 2 files

To run CMake, you need to switch to your build directory on AlmaLinux 8:

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!

That’s it. You are done with Install and Use CMake on AlmaLinux 8

Conclusion

At this point, you have learned to Install and Use CMake on AlmaLinux 8. As you saw, you can easily download the latest package of CMake and start to build your projects with this amazing tool.

Hope you enjoy it. You may also interested in these articles:

Install and Use Gulp.js on AlmaLinux 8

How To Set up Anaconda on AlmaLinux 8

Install and Run Xfce Desktop on AlmaLinux 8

How To Install Wine on AlmaLinux 8

FAQs

What is the latest version of CMake available on AlmaLinux 8?

The version of CMake available in the AlmaLinux 8 repositories may not be the latest. If you need a more recent version, you can install it from the source as described in the above guide steps (Install and Use CMake on AlmaLinux 8).

How to generate a Makefile with CMake?

You can generate a Makefile using CMake with the following command inside the project directory:
cmake .

What are some common issues when using CMake on AlmaLinux 8?

Common issues include missing dependencies or outdated CMake versions. You need to be sure all required libraries are installed and using an appropriate version of CMake for your project. The latest version setup is described in the above guide on Install and Use CMake on AlmaLinux 8.

Can We use CMake to build projects with different compilers?

Yes, CMake is compiler-independent and can be configured to use different compilers such as GCC or Clang.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!