CMake -Beginners: How to build a project using CMake

CMake is an opensource tool to create a build process for any kind of projects across different platforms. It can create build files for Linux(makefiles) and windows(workspaces). It automates the generation of build systems such as Makefiles and Visual Studio project files saving a huge amount of time in a build process development.

In this article, I will show you how to use Cmake for Linux based systems. Our project is rather simple. A real project would be more complicated than the one we’ve created, but once you learn how to use it for a simple project, you can magnify it for a complex project.

To start using CMake, first install it using the following command in a Linux based system.

sudo apt-get install cmake

Check the version of CMake installed

cmake --version

It’s time to build the first project with CMake. First, you have to create a file called CMakeLists.txt for your project, it's a mandatory file for every single project you want to build with CMake.

Let's look at the basic contents of CMakeLists.txt for a project with two directories. One containing the main file and the other with the dependent source code which would be called from the main file. This CMakeLists.txt is similar to a project I have created on Github. You can get it from here for practice https://github.com/alekarasma/CMake_example.

cmake_minimum_required(VERSION 2.8.9)#name of the project
project(project_name)
#Bring the headers into the projectinclude_directories(pathtoinclude_dir/Inc) #You can manually add the sources using the set command
#I prefer file(GLOB...) which allows for wildcard additions as #follows:
file(GLOB SOURCES "pathtosrc_dir/Src/*.c")
file(GLOB SOURCES1 "pathtomainfile_dir/*.c")
#name of executable, source files for executable
add_executable(executable_name ${SOURCES} ${SOURCES1})
  • The first line sets the minimum version of CMake required to build this project. If the current version of CMake is lower than that required it will stop processing the project and report an error.
  • the second line sets the name of the project.
  • projectinclude_directories command is to add the path of include directories.
  • Adding source files -CMake allows different ways to add source files

we have use GLOB method or the other is the set method to add each file manually.

set(SOURCE
${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
)
  • The add_executable method sets the name of the executable and sources to create executable.

Once the project is created like this, I practised creating a build folder in the project folder— it will contain all build related files. Simply run cmake .. from the build folder and it will create the Makefile for the project. Run make to build your project. Steps as follows:

$ mkdir build
$ cd build
$ cmake ..
$ make

Yay!!!. You have created the first executable with CMake.

To create a static and dynamic library through CMake. You can check out an example here https://github.com/alekarasma/CMake_example/tree/master/CMake_Lesson2. Go through CMakeLists.txt, I have commented it to make it easy to understand how to create a shared or static library from the source files.

Happy learning!!!. Please feel free to share your feedback.