Introduction to CMake build system and CMakeLists file.

CMake is the meta build system widely used for C and C++ projects and it generates build scripts for target platform. CMake is platform independent and generates Makefile on the Linux.

When the cmake command is launched it parses an instruction file called CMakeLists.txt. The name of the file is case sensitive and should be CMakeLists.txt for cmake to be able to parse it.

  1. Build simple executable file from source files:

Lets create a simple CMakeLists.txt file which builds C++ program with two source files.

In general very first instruction used in CMakeList.txt file is specifying the minimum cmake version required for our project. When cmake is run if the version is not matched it will throw fatal error.

cmake_minimum_required( VERSION 3.5 )

Next instruction is declaring the name of the project and programming language C or C++ etc…

project (cmake_c2p LANGUAGES CXX)

In the above instruction CXX specifies C++ and simple C can be used for C language. CMake supports C, C++, CUDA, Fortran etc. By default C, C++ languages are supported if there is no LANGUAGE instruction is specified.

Next instruction is create a build target, in this case it is an executable which is generated by compiling and linking two C++ files.

add_executable (hello_cmake hello_1.cpp hello_2.cpp)

Now we assume that source file and CMakeLists.txt file are in the same directory. To build using cmake create build directory and goto that directory using the below commands. This is one more feature/advantage of CMake which builds out side of the source directory. The advantage with this kind of build system is it will not mess up the source directory with build files.

The example C++ code we are using for this blog is given below.

//Hello_1.hpp file content
int largest(int num1, int num2);

//hello_1.cpp file content
#include <iostream>
#include "hello_1.hpp"

using namespace std;
int main() {
   cout << "The largest number is: " << largest(34,65) << endl;
}


//hello_2.cpp file content

#include "hello_1.hpp"

int largest(int num1, int num2) {
        return num1 > num2 ? num1:num2;
}

mkdir build
cd build

demo@c2plabs:~/CMAKE_BLOG/build$ cmake --build .
[ 33%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake

Above commands show the build process using cmake, when we run the cmake with build options it generates executable “hello_cmake” as specified in add_executable instruction. The list of files generated by cmake on Linux platform is as blow

  • Makefile : makefile generated which contains instructions for make command
  • CMakeCache.txt: The is cache file consists of instructions used by cmake when we re-run it.
  • CMakeFiles: Directory which contains temporary files, used by CMake for detecting the operating system, compiler, and so on.
  • cmake_install.cmake: A CMake script handling install rules, which is used at install time.
  • And executable file as instructed in add_executable instruction.

Leave a Reply

Your email address will not be published. Required fields are marked *