- Installing CMake
- Windows
- macOS
- Linux, UNIX
- Download Verification
- Understanding The Basics
- So what a makefile does?
- The concept of CMake
- Edit your CMakeLists.txt
- Another Example — Linking more than 1 file
- Libraries and Subdirectories
- Levels and Libraries
- Reference
- Install and Use CMake on Ubuntu 22.04
- Install and Use CMake on Ubuntu 22.04
- Install CMake 3 on Ubuntu 22.04
- Download CMake LTS on Ubuntu
- Build and Install CMake on Ubuntu
- How To Use CMake
- Conclusion
Installing CMake
There are several ways to install CMake, depending on your platform.
Windows
There are pre-compiled binaries available on the Download page for Windows as MSI packages and ZIP files. The Windows installer has an option to modify the system PATH environment variable. If that is not selected during installation, one may manually add the install directory (e.g. C:\Program Files\CMake\bin) to the PATH in a command prompt.
One may alternatively download and build CMake from source. The Download page also provides source releases. In order to build CMake from a source tree on Windows, you must first install the latest binary version of CMake because it is used for building the source tree. Once the binary is installed, run it on CMake as you would any other project. Typically this means selecting CMake as the Source directory and then selecting a binary directory for the resulting executables.
macOS
There are pre-compiled binaries available on the Download page for macOS as disk images and tarballs. After copying CMake.app into /Applications (or a custom location), run it and follow the “How to Install For Command Line Use” menu item for instructions to make the command-line tools (e.g. cmake) available in the PATH. Or, one may manually add the install directory (e.g. /Applications/CMake.app/Contents/bin) to the PATH.
One may alternatively download and build CMake from source as in the following section.
Linux, UNIX
There are pre-compiled binaries available on the Download page for some UNIX platforms. One may alternatively download and build CMake from source. The Download page provides source releases. There are two possible approaches for building CMake from a source tree. If there is no existing CMake installation, a bootstrap script is provided:
./bootstrap make make install
(Note: the make install step is optional, cmake will run from the build directory.)
By default bootstrap will build CMake without any debug or optimization flags. To enable optimizations you will need to specify the CMAKE_BUILD_TYPE option to bootstrap like this: ./bootstrap — -DCMAKE_BUILD_TYPE:STRING=Release
For more options with bootstrap, run ./bootstrap —help .
Or, an existing CMake installation can be used to build a new version:
(Note: the make install step is optional, cmake will run from the build directory.) If you are not using the GNU C++ compiler, you need to tell the bootstrap script (or cmake) which compiler you want to use. This is done by setting the environment variables CC and CXX before running it. For example:
env CC=cc CXX=CC ./bootstrap make make install
Download Verification
Each release on the Download page comes with a file named cmake-$version-SHA-256.txt, where $version is the release version number.
One may use this file to verify other downloads, such as the source tarball. For example:
$ curl -OL https://github.com/Kitware/CMake/releases/download/v3.20.1/cmake-3.20.1-SHA-256.txt $ curl -OL https://github.com/Kitware/CMake/releases/download/v3.20.1/cmake-3.20.1.tar.gz $ sha256sum -c --ignore-missing cmake-3.20.1-SHA-256.txt cmake-3.20.1.tar.gz: OK
The SHA-256 file itself can be verified by GPG signature:
$ curl -OL https://github.com/Kitware/CMake/releases/download/v3.20.1/cmake-3.20.1-SHA-256.txt.asc $ gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys C6C265324BBEBDC350B513D02D2CEF1034921684 $ gpg --verify cmake-3.20.1-SHA-256.txt.asc cmake-3.20.1-SHA-256.txt
The GPG key C6C265324BBEBDC350B513D02D2CEF1034921684 is a signing subkey whose expiry is updated yearly.
Understanding The Basics
To illustrate what makefile does, we first create a project folder (I named it CMakeTuto here).
Then use VSCode to open the folder.
mkdir CMakeTuto
cd CMakeTuto/
code .
Note here makefile do not accept spaces. It only accept tabs.
#include
int main(){
std::cout «Hello World!\n»;
return 0;
}
Now your CMakeTuto directory should be like this:
So what a makefile does?
Use the make command to run the makefile
make command basically run the command in makefile . (that is g++ main.cpp -o out in this case. )
Now a out file should appear. Run the out file.
It should print the “Hello World!”.
The concept of CMake
CMake makes your makefile.
now we can first delete our makefile and out in the previous section.
First check our cmake version
You can also use the command cmake to check the usage.
Common usage: cmake [options]
Create a file CMakeLists.txt , leave the file blank.
Now your directory should be:
Lets create a directory build.
Note if you want to use cmake command, you need to go to the build directory (which is build ).
Now your directory should be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.10.2
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── tmp
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── feature_tests.bin
│ │ ├── feature_tests.c
│ │ ├── feature_tests.cxx
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ └── Makefile
├── CMakeLists.txt
└── main.cpp
8 directories, 24 files
Notice now the Makefile is generated.
If You look into that Makefile and you will see it just build what our CMakeLists.txt require.
(Note we haven’t write anything into CMakeLists.txt yet)
We won’t teach those generated files.
Edit your CMakeLists.txt
Now write into your CMakeLists.txt .
cmake_minimum_required(VERSION 3.9.0)
project(HelloWorld) #replace «HelloWorld» with your project name
add_executable(${PROJECT_NAME} main.cpp)
run the cmake command in your build folder again.
If You look into that Makefile again and you will see it just build what our CMakeLists.txt require.
Lets run the Makefile using make command.
You should see this outcome:
Scanning dependencies of target HelloWorld
[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/main.cpp.o
[100%] Linking CXX executable HelloWorld
[100%] Built target HelloWorld
And now you should see a HelloWorld executable is in the build folder.
Lets run the HelloWorld executable.
Now you know the basics of cmake.
Another Example — Linking more than 1 file
.
├── adder.cpp
├── CMakeLists.txt
└── main.cpp
float add(float a, float b)
{
return a+b;
}
cmake_minimum_required(VERSION 3.9.0)
project(HelloWorld) #replace «HelloWorld» with your project name
add_executable(${PROJECT_NAME} main.cpp adder.cpp) #add the files here
//function prototype
float add(float a, float b);
#include
int main(){
std::cout add(6.9, 13.2)
return 0;
}
Build and run the program
mkdir build
cd build
cmake ..
make
./HelloWorld
Libraries and Subdirectories
Levels and Libraries
In practical situations we often put our files into a folder.
We need to put a sublevel CMakeLists.txt in each subfolder.
.
├── Adder
│ ├── adder.cpp
│ ├── adder.h
│ └── CMakeLists.txt
├── CMakeLists.txt
└── main.cpp
1 directory, 5 files
#include «adder.h»
float add(float a, float b)
{
return a+b;
}
#include
#include «Adder/adder.h»
int main(){
std::cout add(6.9, 13.2)
return 0;
}
cmake_minimum_required(VERSION 3.9.0)
project(HelloWorld) #replace «HelloWorld» with your project name
add_executable(${PROJECT_NAME} main.cpp) #build the main
add_subdirectory(Adder) #build the libraries
target_include_directories(${PROJECT_NAME} PUBLIC Adder)
#—uncomment this if your cmake version >3.13.0
#—target_link_directories(${PROJECT_NAME} PRIVATE Adder)
target_link_libraries(${PROJECT_NAME} adder) #links the libraries to core project
the sublevel Adder/CMakeLists.txt
add_library(adder adder.cpp) #build as a library in the most simplistic way possible
Build and run the program
mkdir build && cd build
cmake ..
make
./HelloWorld
Reference
Install and Use CMake on Ubuntu 22.04
In this guide from the Linux Tutorials, we want to teach you to Install and Use CMake on Ubuntu 22.04.
CMake is an open-source, cross-platform, and free tool (or meta-build system), that some would describe as a build system generator that is used in conjunction with your build environment.
It is utilized to control the build process (perform build configuration) by using configuration files or what you would call scripts (CMakeLists.txt) to generate build files. In addition to supporting various build systems and operating systems, it also supports several programming languages such as C++, C#, Cuda, and more.
Install and Use CMake on Ubuntu 22.04
To install CMake, you need to 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 Ubuntu 22.04.
Now follow the steps below to complete this guide.
Install CMake 3 on Ubuntu 22.04
First, update your local package index with the following command:
Next, install the required packages and dependencies on Ubuntu 22.04:
sudo apt install build-essential checkinstall zlib1g-dev libssl-dev -y
To install the latest CMake version, you need to visit the CMake downloads page.
Download CMake LTS on Ubuntu
Then, use the wget command to download the latest stable version of CMake:
wget https://github.com/Kitware/CMake/releases/download/v3.23.2/cmake-3.23.2.tar.gz
Extract your downloaded file:
Next, switch to your CMake directory on Ubuntu 22.04:
At this point, use the following commands to install CMake:
The Bootstrap script may take a few minutes.
Build and Install CMake on Ubuntu
Use the make or gmake command to build your process:
Now install CMake using the following:
This process can take a few minutes to almost ten minutes.
Verify your CMake installation on Ubuntu 22.04 by checking its version:
Output cmake version 3.23.2 CMake suite maintained and supported by Kitware (kitware.com/cmake).
When your installation is completed, let’s see how to use the CMake.
How To Use CMake
At this point, we want to teach you how to use CMake by creating a sample project hello world on Ubuntu 22.04.
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:
#include int
main() std::cout < "Hello World!"
return
0 ;
>
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.
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:
[olivia@ cmproject]# tree . ├── build ├── CMakeLists.txt └── hello.cpp 1 directory, 2 files
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.
Output Hello World!
Conclusion
At this point, you learn to Install and Use CMake on Ubuntu 22.04.
You may be interested in this article How To Install CMake on Ubuntu 20.04.