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 ;
}
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
[olivia@ cmproject]# tree . ├── build ├── CMakeLists.txt └── hello.cpp 1 directory, 2 files
cd
build
cmake ..
..
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 .
.
/hello
Output
Hello World!
Thanks for sharing. How to set cmake3 as the default camke? When I type cmake –version, it still shows the version 2. I need to type cmake3 –version, it will print the version 3.
looks like a newer version of binary cmake placed in the $PATH after the old one or not in the $PATH at all. To check what verstion takes precedence try run following command:
$ which cmake
to see other versions:
$ whereis cmake
fix your PATH accordingly (in your system-wide profile or your personal ~/.bashrc) and reload bash by exec bash or close and reopen the terminal window.
Anyway, you may always execute cmake by full (absolute) path:
$ cd your-project-src
$ mkdir build
$ cd build
$ /full/path/to/cmake ..