In this article, we’ll learn to install cmake on Linux. CMake is 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. Introduction to modern CMake for beginners A look at one of the most popular build systems for C and C. CMakeis a collection of open-source and cross-platform tools used to build and distribute software. A Basic Starting Point (Step 1) ¶ The most basic project is an executable built from source code files. For simple projects, a three line CMakeLists.txt file is all that is required. This will be the starting. CMake is a sophisticated, cross-platform, open-source build system developed by Kitware in 2001. CMake is the name used to refer to the entire family of tools: CMake, CTest, CDash, and CPack. These tools are used to CMake – An intelligent build system used to build simple, to very elaborate projects.
- Cmake Tutorial For Beginners Pdf
- Cmake For Windows Tutorial
- Cmake For Beginners Online
- Cmake For Cygwin
- Cmake For Beginners Youtube
- Easy Things To Make For Beginners
- Cmake For Beginners Free
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.
May 11, 2017 CMake is a versatile tool that helps you build C/C projects on just about any platform you can think of. It’s used by many popular open source projects including LLVM, Qt, KDE and Blender. All CMake-based projects contain a script named CMakeLists.txt, and this post is meant as a guide for configuring and building such projects.
How to Install CMake on Linux?
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. 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
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:![Beginners Beginners](https://api-2d3d-cad.com/wp-content/uploads/2064.jpg)
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.
This tutorial will guide you through the process of creating and developing a simple CMake project. Step by step, we will learn the basics of CMake as a build system, along with the CLion settings and actions for CMake projects.
The source code of the sample project used below is available on GitHub.
1. Basic CMake project
CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists.txt file is automatically generated under the project root.
Sfml for visual studio 2019. Let’s start with creating a new CMake project. For this, go to File | New Project and choose C++ Executable. In our example, the project name is cmake_testapp and the selected language standard in C++14.
By default, we get the project with a single source file main.cpp and the automatically created root CMakeLists.txt containing the following commands:
Command | Description |
---|---|
cmake_minimum_required(VERSION 3.13) | Specifies the minimum required version of CMake. It is set to the version of CMake bundled in CLion (always one of the newest versions available). |
project(cmake_testapp) | Defines the project name according to what we provided during project creation. |
set(CMAKE_CXX_STANDARD 14) | Sets the CMAKE_CXX_STANDARD variable to the value of 14, as we selected when creating the project. |
add_executable(cmake_testapp main.cpp) | Adds the cmake_testapp executable target which will be built from main.cpp. |
2. Build targets and Run/Debug configurations
Target is an executable or a library to be built using a CMake script. You can define multiple build targets in a single script.
For now, our test project has only one build target, cmake_testapp. Upon the first project loading, CLion automatically adds a Run/Debug configuration associated with this target:
Click Edit Configurations in the switcher or select Run | Edit Configurations from the main menu to view the details. The target name and the executable name were taken directly from the CMakeLists.txt:
Notice the Before launch area of this dialog: Build is set as a before launch step by default. So we can use this configuration not only to debug or run our target but also to perform the build. To learn more about various build actions available in CLion, see Build actions.
3. Adding targets and reloading the project
Now let’s add another source file calc.cpp and create a new executable target from it.
Right-click the root folder in the Project tree and select New | C/C++ Source File. CLion prompts to add the file to an existing target:
Since our goal is to create a new target, we clear the Add to targets checkbox. Accordingly, CLion notifies us that the new file currently does not belong to any target:
Now let's declare a new target manually in the CMakeLists.txt. Note that CLion treats CMake scripts as regular code files, so we can use code assistance features like syntax highlighting, auto-completion, and navigation: When we make changes in CMakeLists.txt, CLion needs to reload it in order to update the project structure:
We can either reload the project once (Reload changes) or enable automatic reload to let CLion silently apply all the changes in CMakeLists.txt. The option for enabling/disabling auto-reload is also available in Settings / Preferences | Build, Execution, Deployment | CMake.
After reloading the project, CLion adds a Run/Debug configuration for the new target:
Library targets
Cmake Tutorial For Beginners Pdf
Up to this point, the targets we added were executables, and we used
add_executable
to declare them. For library targets, we need another command - add_library. As an example, let's create a static library from the calc.cpp source file: As well as for executables, CLion adds a Run/Debug configuration for the library target after reloading the project:
However, this is a non-executable configuration, so if we attempt to run or debug it, we will get the Executable not specified error message.
To obtain the library file, we need to build the test_library target. For this, we can switch to the corresponding configuration and press , or call Build | Build 'test_library'. The libtest_library.a file will appear in the cmake-build-debug folder.
4. Build types and CMake profiles
All the Run/Debug configurations created by far were Debug configurations, which is the default build type of the CMake profile that was automatically configured for our project. CMake profile is a set of options for the project build. It specifies the toolchain, build type, CMake flags, path for storing build artifacts, make build options, and environment variables.
For example, to separate the Debug and Release builds, we can add a new CMake profile in Settings / Preferences | Build, Execution, Deployment | CMake and set its build type to Release:
Notice the Build directory field that specifies the location of build results. The default folders are cmake-build-debug for Debug profiles and cmake-build-release for Release profiles. You can always set other locations of your choice.
Now the Run/Debug configuration switcher shows two available profiles:
Switching configurations or CMake profiles may affect preprocessor definitions used while resolving the code. For example, when there are separate flags for Debug and Release builds, or when there are some variables that take different values depending on the build type. This is called resolve context.
Resolve context defines how CLion performs syntax highlighting and other code insights like Find Usages, refactorings, and code completion. When you switch between configurations, the resolve context for the current file is changed automatically. Also, you can select it manually in the context switcher (<Select automatically> restores the automatic selection):
5. Adding include directories
In order to use additional headers located in separate directories, we need to add them either to all the targets or to some specific ones.
For example, let’s create three directories under the project root, includes, includes/general, includes/math, and write the following commands in CMakeLists.txt:
include_directories(includes/math)include_directories(includes/general)
These two commands make the headers located in general and math available for including from the sources of all targets. For example, we can write
#include 'header_math.h'
in calc.cpp. Headers and sources that you add to the project will be resolved correctly only if you include them explicitly in CMakeLists.txt or if you include them in other files already belonging to the project (see Managing CMake project files ).
6. Linking libraries
Static libraries
Cmake For Windows Tutorial
On step 3, we created a static library called test_library (the default filename is libtest_library.a ).
Let's create a lib directory under the project root and copy libtest_library.a from its default location (cmake-build-debug) to this folder.
We will use two commands to link our static library to the cmake_testapp target: find_library provides the full path, which we then pass directly into the target_link_libraries command via the
${TEST_LIBRARY}
variable: Note: make sure to place
target_link_libraries
after the add_executable
command, so that CMake actually builds the target before linking the library.Cmake For Beginners Online
Dynamic libraries (Boost example)
To illustrate linking dynamic libraries, we will take an example of using Boost.Test framework.
Let's write a simple function
int add_values (int a, int b) { return a+b;}
in calc.cpp and create the associated header calc.h with the function declaration. We will test this function with the help of Boost.Test framework.For details on working with Boost.Test, see Unit testing tutorial.
As our project gets more complicated, the root CMakeLists.txt file can become difficult to maintain. To avoid this, and to build a transparent project structure, we will extract the tests into a subproject.
Let's add a directory called test and create a source file tests.cpp within it. Also, we need to provide this directory with its own CMakeLists.txt file (right-click test in the Project tree and select New | CMakeLists.txt ):
The subdirectory test/CMakeLists.txt script is initially empty. We can start filling it up by inserting a live template for Boost with libs. Press Ctrl+J or click Code | Insert Live Template, and choose
boost_with_libs
: Adjust the inserted code to the following:
set(Boost_USE_STATIC_LIBS OFF) #enable dynamic linking # search for unit_test_framework find_package(Boost REQUIRED COMPONENTS unit_test_framework) include_directories(${Boost_INCLUDE_DIR}) # create a cmake_testapp_boost target from test.cpp add_executable(cmake_testapp_boost tests.cpp) # link Boost libraries to the new target target_link_libraries(cmake_testapp_boost ${Boost_LIBRARIES}) # link Boost with code library target_link_libraries(cmake_testapp_boost test_library)
Also, we need to place the
add_subdirectory(test)
command in the rootCMakeLists.txt to make our test target cmake_testapp_boost available for the main build.This command, when placed in the root CMake script, declares a subproject test that has its own CMakeLists.txt.
After reloading the changes in both CMakeLists.txt files, CLion creates a Run/Debug configuration for the cmake_testapp_boost target. This is a regular CMake Application configuration which we could run/debug right away. However, to be able to use the built-in test runner, let's create another configuration out of the Boost.Test template:
Now we can run this configuration and get test results. Test runner shows the tree of tests in the suite, their output, status, and duration:
Cmake For Cygwin
7. Working with CTest
This chapter gives a simple example of how to use CTest, a framework for compiling and running tests as part of the CMake build process. Find general description of the framework in CTest support.
Add CTest to the sample project
- Create a subdirectory inside test and call it ctest.
- Add two source files, addvalues_zero.cpp and addvalues_negpos.cpp, a header file (where we will place the assertion macro) assert_macro.h, and a CMakeLists.txt script:
- Add the following lines to ctest/CMakeLists.txt:cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR) add_executable(ctest_exe_addvalues_zero addvalues_zero.cpp) add_executable(ctest_example_addvalues_negpos addvalues_negpos.cpp) add_test(ctest_addvalues_zero ctest_exe_addvalues_zero) add_test(ctest_addvalues_negpos ctest_example_addvalues_negpos)The first line states the minimum supported version of CTest, which corresponds to the version of CMake, 3.14.We are using the add_test command here to register our executables,
ctest_exe_addvalues_zero
andctest_example_addvalues_negpos
, with CTest. - Now we can place the actual code inside the test sources:
- assert_macro.h:#include <iostream> #include <sstream> #define assertEqual( .. ) do { if( !( __VA_ARGS__ ) ) { std::cerr << 'Unit test assert [ ' << ( #__VA_ARGS__ ) << ' ] failed in line [ ' << __LINE__ << ' ] file [ ' << __FILE__ << ' ]' << std::endl; err_code = 1; } } while( false )
- addvalues_zero.cpp:#include 'assert_macro.h' int test_addvalues_zero() { int err_code = 0; assertEqual (0+0 0); assertEqual ((-5)+5 0); return err_code; } int main() { return test_addvalues_zero(); }
- addvalues_negpos.cpp:#include 'assert_macro.h' int test_addvalues_negpos() { int err_code = 0; assertEqual ((-5)+10 5); assertEqual ((-10)+5 -5); assertEqual (5+(10) 5); //test to fail return err_code; } int main() { return test_addvalues_negpos(); }
- Next, we need to enable CTest and declare the subproject in the top-level CMakeLists.txt:The enable_testing command creates a built-in target
test
which will execute CTest. - Reload the project.
Cmake For Beginners Youtube
After the reload, CLion detects the tests and creates a ready-to-go All CTest configuration:
If we run this configuration, the results will be shown in the Test Runner window, similarly to other supported testing frameworks:
We can also use the gutter menu next to the
add_test
commands in ctest/CMakeLists.txt to run or debug our tests: In case of running/debugging a test via the gutter menu, CLion creates a temporary configuration, so if we go to Edit Configuration, we will find two automatically generated configurations of the CTest Application type:
Easy Things To Make For Beginners
Let's check the All CTest configuration. Click the pen icon next to the Test list to run field. This opens the List of Available Tests dialog, where we can change the set of tests:
For more information on the CTest Application template, see CTest run/debug configuration.
8. Learn more
To dig deeper into CMake in CLion, learn how to:
Cmake For Beginners Free
- Run Build actions and CMake install
- Use environment variables and the CLION_IDE macro.
Last modified: 08 March 2021