In this article, we'll learn to install cmake on Linux. CMakeis a cross-platform open-source meta-build system that can build, test, and package software. It can be used to support multiple native build environments including make in Linux/Unix, Apple's Xcode, and Microsoft Visual Studio.
How to Install CMake on Linux?
Explanations how to install the project's dependencies, compile it and run tests (uses C17, CMake, Boost libraries (asio, program options, etc.), Google test) Quizzes and final test (some have additional code examples) Programming assignments (work on the project, etc.) This course is for you if you. Udemy is an online learning and teaching marketplace with over 130,000 courses and 35 million students. Learn programming, marketing, data science and more. ️ Understanding CMake and writing recipes for cmake. ️ Adding your own custom logo on boot. ️ Most important concept of Yocto: bbappends. Course Curriculum in Deep. FILES and PACKAGES: a) FILES variable. B) PACKAGES variable. C) How to create your own package. Static and Dynamic Library: a) Yocto recipe for static library. The CMake Tutorial is now available as the CMake Tutorial Guide in the official documentation. Kitware also provides online and onsite CMake trainings.
If you are to work with CMake in Linux, you'll probably need a C/C++ compiler and the make
build system since CMake generates ‘Makefiles' on Linux. A Makefile contains the steps for compiling the program and generating an executable. The installation steps for these tools depend on the distribution.
We will be looking at two ways to install CMake.
1. Using Package Managers like apt/dnf
Note: Installing this way, the version of CMake installed will generally be an older one.
For Ubuntu/Debian (and their derivatives)
We can obtain information of a package and its dependencies using the apt command. Doing that for cmake
:
As is highlighted in the screenshot above, cmake
recommends installing gcc
(GNU Compiler Collection) and make
. It also suggests some packages which are optional. Ps4 media server app. You'll need to install g++
if you're working on a C++ project.
To install cmake , g++ and make using the apt command, type:
Note that while installing these packages, the gcc
package is also installed.
For Fedora/CentOS (RedHat based distros and their derivatives)
To install cmake
, g++
and make
using the dnf
command, type:
2. Using CMake's Official Website
CMake's official website has two options to install CMake on Linux as of now:
- A shell script (
.sh
file) - A
.tar.gz
archive
Cmake Udemy Promo Code
Installing through either of them will get you the latest version of CMake. You'll still need a compiler (gcc
/g++
) and make
. You can install them using the package manager. For Ubuntu/Debian based distros, type:
For RedHat Based distros, type:
For CentOS 8 based systems, we'll be using the shell script. You can either download the script by clicking on the .sh
file link on the website or by using the wget
command :
Note: The link is for the latest version of CMake as of writing this article and may change in the future.
It is in general a good idea to inspect scripts before running them. To do that you can open it in the editor of your choice. Using the nano editor to open it:
Going through the script, it also contains the .tar.gz
archive for the CMake binary. Hence, it's size (~40MB). The script acts like a ‘self extracting archive'. Now that we've inspected the script, we can run it from the current directory using:
The above command will install cmake
globally for all users to /usr/local/bin
and the exclude-subdir
option is to get rid of the extra directory that is produced while extracting the .tar.gz
archive.
(You need the tar
command as the script uses it. If prompted tar: command not found
, install it by typing $ sudo dnf install tar
)
Also, cmake
will not be managed by the system package manager when installed this way. To update cmake
you'll need to repeat this process for any new versions.
A Sample CMake project
We'll create a simple C++ Hello World program which uses CMake. Let's start by creating a different directory for our project. Using the mkdir and cd commands:
Currently, the directory is empty. We'll now create a C++ source file named hello_world.cpp
which, as the name suggests, will print just Hello World!
You can use the editor of your choice. I'll be using nano:
Now we'll create a CMakeLists.txt
file(with this exact capitalization) which is required by CMake:
The root directory of the project ( ~/projectzero
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:
It's a good practice to have a separate build
directory for executables. So, let's do that:
The project structure looks like this now:
To run cmake
we need to change into the build
directory:
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.
Running cmake .
generated the configuration files for CMake in projectzero/build
. We can list the contents of build
directory using the ls command:
Now we can generate the executable simply by typing:
Run the executable by typing:
Congratulations on building your first CMake project. You can find more such beginner-friendly CMake projects on this GitHub profile.
Conclusion
In this article we learnt how to install CMake on Linux and created a simple Hello World program using CMake. CMake is a very useful tool for C/C++ development. You can find more information about CMake in its documentation.
Introduction to CMake Buildsystems¶
A buildsystem describes how to build a project's executables and librariesfrom its source code using a build tool to automate the process. Forexample, a buildsystem may be a Makefile
for use with a command-linemake
tool or a project file for an Integrated Development Environment(IDE). In order to avoid maintaining multiple such buildsystems, a projectmay specify its buildsystem abstractly using files written in theCMakelanguage
. From these files CMakegenerates a preferred buildsystem locally for each user through a backendcalled a generator.
To generate a buildsystem with CMake, the following must be selected:
The top-level directory containing source files provided by the project.The project specifies its buildsystem using files as described in thecmake-language(7)
manual, starting with a top-level file namedCMakeLists.txt
. These files specify build targets and theirdependencies as described in the cmake-buildsystem(7)
manual.
The top-level directory in which buildsystem files and build outputartifacts (e.g. executables and libraries) are to be stored.CMake will write a CMakeCache.txt
file to identify the directoryas a build tree and store persistent information such as buildsystemconfiguration options.
To maintain a pristine source tree, perform an out-of-source buildby using a separate dedicated build tree. An in-source build inwhich the build tree is placed in the same directory as the sourcetree is also supported, but discouraged.
Cmake Udemy Courses
This chooses the kind of buildsystem to generate. See thecmake-generators(7)
manual for documentation of all generators.Run cmake--help
to see a list of generators available locally.Optionally use the -G
option below to specify a generator, or simplyaccept the default CMake chooses for the current platform.
Cmake Udemy App
When using one of the Command-Line Build Tool GeneratorsCMake expects that the environment needed by the compiler toolchainis already configured in the shell. When using one of theIDE Build Tool Generators, no particular environment is needed.