Install and Use CMake on Centos 7

In this article, we want to teach you to Install and Use CMake on Centos 7.

CMake is an open-source, cross-platform tool that uses compiler and platform-independent configuration files to generate native build tool files specific to your compiler and platform. The CMake Tools extension integrates Visual Studio Code and CMake to make it easy to configure, build, and debug your C++ project.

The “CMake” executable is the CMake command-line interface. It may be used to configure projects in scripts. It is a cross-platform build system generator. Projects specify their build process with platform-independent CMake list files included in each directory of a source tree with the name CMakeLists. txt.

Steps To Install and Use Latest CMake on Centos 7

Before you start to install CMake on Centos 7, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Centos 7.

Now follow the steps below to complete this guide.

Install CMake on Centos 7

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

Download CMake 3 on Centos

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

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

Extract your downloaded file:

tar -zxvf cmake-3.22.3.tar.gz

Next, switch to your CMake directory on Centos 7:

cd cmake-3.22.3

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

# ./bootstrap
# make
# sudo make install

These commands will take some time to complete.

Verify your CMake installation on Centos 7 by checking its version:

cmake --version

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

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 Centos 7.

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:
[olivia@ cmproject]# tree
.
├── build
├── CMakeLists.txt
└── hello.cpp

1 directory, 2 files
To run CMake, you need to switch to your build directory on Centos 7:
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 Centos 7.
Hope you enjoy it.
You may like these articles:

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!