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:
Extract your downloaded file:
tar -zxvf cmake-3.22.3.tar.gz
Next, switch to your CMake directory on Centos 7:
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:
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:
Add the following content to the file:
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:
project(MyProject VERSION 0.0.1)
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:
[olivia@ cmproject]# tree . ├── build ├── CMakeLists.txt └── hello.cpp 1 directory, 2 files
.. 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.
cmake --build .