

At its core, CMake isn’t an extremely complicated tool to use however, it does provide a lot of options for you to choose from and use to customize your build process. Advancing With CMakeĪt this point, you’ve seen everything that you need in order to start adding CMake to your projects.

This means you can run make, and you should find the binary executable in the build directory. If you’re using CMake on Linux, you’ll find a Makefile in your build directory. The specific files generated will depend on your operating system (which is the entire point of using CMake). This is where the important distinction mentioned in the introduction comes into play: CMake does not build your application ithelps you build your application. , you should find that the build directory has been filled with a bunch of files but not any executables. Doing so makes it easy to wipe clean all the files generated by CMake.Īfter running cmake. Note: It’s not required, but it’s recommended to create a build directory. communicates that it should look in the parent directory for the CMakeLists.txt file. To do this, create a build folder in your root directory and enter it. Now, you’ve created a complete, albeit very basic, CMakeLists.txt file, and you can finally run cmake. The next version of CMake to come out is v3.17, which is the minimum version needed here:Īdd_executable(add src/main.cpp src/add.cpp) This article uses GCC v9.3 to compile, which came out in March 2020. This is because CMake needs to know the various options and flags of your compiler. The general rule of thumb is that you want to specify a version of CMake that is later than your compiler. The first of these is the cmake_minimum_required() line.Īs can likely be assumed from the name of the command, this specifies the lowest version that you want to support. The CMakeLists.txt file has a very long list of possibilities, but there are a few things that every CMakeLists.txt file needs to have. Here, you’ll define the name of your application, what files are included, if any libraries are needed, and more.

This file defines everything related to your use of CMake. To begin, you need to create the file CMakeLists.txt in the root directory of your application. Now that you’ve created the sample application, it’s time to add CMake to the project. If you do run into any issues, please check that your compiler is the latest version and that you’ve correctly copied the code. The previous command should be able to run without any issues. For the rest of this tutorial, your OS should be irrelevant. Note: It’s assumed that you’re using Linux and, by extension, GCC (the GNU Compiler Collection) for this part however, please compile for your specific OS. In here, you want to create a folder called src, in which you create two files: main.cpp and add.cpp.

Start off by creating an empty directory and then enter it. For this tutorial, you’re going to build a very simple two-file application that can add two numbers together. Since CMake is a tool that’s meant to aid you in building your applications, you need to have an application to begin with. If you want to clone the project and follow along in your own editor, here is the link to the GitHub repo. Once the basic use case has been covered, you’ll also be introduced to how you can take CMake implementations to the next level.
#CMAKE TUTORIAL HOW TO#
In this tutorial, you’ll be giving an overview of how to use CMake in general, using a simple C++ application as an example. Instead, CMake provides a way for developers to configure a build pipeline easily, as well as make sure it works cross-platform. It’s very important to note that CMake does not build any applications that’s a job for the compiler. There are other options to use for building applications, including autoconf and BJam however, as mentioned before, CMake helps build applications- help being the keyword. Especially in the C++ community, the standard is to use CMake when building your applications. However, with languages that have been around as long as C and C++, the community has converged around some de facto standards. When it comes to packaging an application, there are many ways to do it.
