This tutorial intends to teach you How To Install and Use CMake on AlmaLinux 8.
Let’s see what is exactly CMake means and what does it.
What is CMake?
CMake is known as a meta-build system. It doesn’t actually build your source code: instead, it generates native project files for the target platform. For example, CMake on Windows will produce a solution for 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 separated 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.
How 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 CMake on your server.
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 the Latest CMake, 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:
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:
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 point, 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:
Add the following content to the file:
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:
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.
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:
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:
Run the CMake command:
.. 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:
Output Hello World!
That’s it. You are done.
At this point, you learn to install CMake in the latest version and create your first project on AlmaLinux 8.
Hope you enjoy it.
You may be 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