CMake: build and test cpp files with ease

CMake: build and test cpp files with ease

What is CMake?

CMake is a tool designed to build, test, and package software. It uses a simple scripting language (CMakeLists.txt files) to define the build process. CMake generates native build files for various platforms and IDEs, such as Makefiles for Unix-like systems, Visual Studio solutions for Windows, and Xcode projects for macOS.


A. Basic Concepts:

  1. CMakeLists.txt: This is the main configuration file for CMake. It contains instructions for building your project.

  2. Source Directory and Build Directory: CMake requires a source directory where your project's source code resides and a separate build directory where CMake generates build files.

B. CMakeLists.txt

Create a file named CMakeLists.txt in the root directory of your project. Open CMakeLists.txt in a text editor and define your project. Here's a minimal example for a C++ project:

  • Specify the minimum version of CMake required

cmake_minimum_required(VERSION 3.10)

  • Set the project name and version

project(MyProject VERSION 1.0)

  • Add an executable target

add_executable(my_executable main.cpp)

C. Build a directory

working in bash:

  • Create a separate directory (usually named build) where CMake will generate build files:

    mkdir build
    cd build

  • Inside the build directory, run CMake and specify the path to the source directory:

    cmake ..

  • After running CMake, you can now build your project using the generated build files. The exact commands depend on your platform and the generator used by CMake. For example, on Unix-like systems with Makefiles generated:

    make

D. Creating a library

  • In the CMakeLists.txt file in the MathFunctions directory, we create a library target called MathFunctions with add_library(). The source files for the library are passed as an argument to add_library(). This looks like the following line:

    add_library(MathFunctions MathFunctions.cxx mysqrt.cxx)

  • To make use of the new library we will add an add_subdirectory() call in the top-level CMakeLists.txt file so that the library will get built.

    add_subdirectory(MathFunctions)

  • Next, the new library target is linked to the executable target using target_link_libraries().

    target_link_libraries(Tutorial PUBLIC MathFunctions)

  • Finally we need to specify the library's header file location. Modify the existing target_include_directories() call to add the MathFunctions subdirectory as an include directory so that the MathFunctions.h header file can be found.

    target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/MathFunctions" )

  • Now let's use our library. In tutorial.cxx, include MathFunctions.h:

    #include "MathFunctions.h"

E. Setting personal versions

  • we modify the CMakeLists.txt file to use the project() command to set both the project name and version number. When the project() command is called, CMake defines Tutorial_VERSION_MAJOR and Tutorial_VERSION_MINOR behind the scenes.

    project(Tutorial VERSION 1.0)

  • Then we used configure_file() to copy the input file with the specified CMake variables replaced:

    configure_file(TutorialConfig.h.in TutorialConfig.h)

  • Since the configured file will be written into the project binary directory, we must add that directory to the list of paths to search for include files.We used target_include_directories() to specify where the executable target should look for include files.

    target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" )

  • Next, we need to modify tutorial.cxx to include the configured header file, TutorialConfig.h.

    #include "TutorialConfig.h"


Covered in my GitHub repository.

Thank you.