CMake

Posted on  by 



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. In CMake, installed targets are registered to exports using the EXPORT argument. Exports are therefore just a set of targets that can be exported and installed. Here we just told CMake to install our library and to register the target in the export jsonutils-export. Then we can go ahead and install the export that we defined above.

This is Kitware, Inc.'s third-party APT repository, which we use for hosting our own Ubuntu packages, such as CMake.

We currently support Ubuntu 16.04, 18.04, and 20.04 on our repository. The 16.04 and 18.04 repositories support x86 (32-bit and 64-bit), and the 20.04 repository supports x86 (32-bit and 64-bit) and ARM (32-bit and 64-bit).

To add the repository to your installation, do the following in order:

  1. If you are using a minimal Ubuntu image or a Docker image, you may need to install the following packages:

  2. Obtain a copy of our signing key:

  3. Add the repository to your sources list and update.

    For Ubuntu Focal Fossa (20.04):

    For Ubuntu Bionic Beaver (18.04):

    For Ubuntu Xenial Xerus (16.04):

  4. As an optional step, if you would like to subscribe to release candidates in addition to production releases, you can add our release candidate repository to your sources.

    For Ubuntu Focal Fossa (20.04):

    For Ubuntu Bionic Beaver (18.04):

    For Ubuntu Xenial Xerus (16.04):

    Note that if you add the release candidate repository, you will still need to add the main repository as well, as the release candidate repository does not provide production releases on its own.

  5. As an optional step, we recommend that you also install our kitware-archive-keyring package to ensure that your keyring stays up to date as we rotate our keys. Do the following:

Now you can install any package from our APT repository. As an example, try installing the cmake package:

For all questions and concerns, please contact debian@kitware.com.

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?

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:

CMake

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:

CMakeCMake

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

Using Cmake On Windows

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:

CMake

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.

Cmake.org

Conclusion

Cmake Post_build

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.





Coments are closed