The CMake Configuration

Writing the Main CMake File

To link the Erbsland Regular Expression Library with your application, you’ll create a top-level CMake configuration that ties everything together. This root file is responsible for defining the overall project and pulling in both the library and your application code.

Let’s start by creating the root CMakeLists.txt file.

  1. Create and open the file in your project root:

    $ nano CMakeLists.txt
    
  2. Add the following content:

    <project>/CMakeLists.txt
    cmake_minimum_required(VERSION 3.25)
    project(htmlstats-project)
    
    add_subdirectory(erbsland-cpp-re)
    add_subdirectory(htmlstats)
    

This configuration establishes the basic structure of your build:

  • Specifies the minimum required CMake version This ensures that all features used by the project are available.

  • Defines the project name The name htmlstats-project is used internally by CMake and build tools.

  • Adds subdirectories in a defined order

    • erbsland-cpp-re: The regular expression library.

    • htmlstats: Your application code.

CMake processes these directories sequentially, which guarantees that the library targets are fully defined before your application is built and linked against them.

Setting Up the Application CMake File

With the top-level configuration in place, you can now define how your application itself is built.

  1. Create and open the application-level CMake file:

    $ nano htmlstats/CMakeLists.txt
    
  2. Add the following content:

    <project>/htmlstats/CMakeLists.txt
    cmake_minimum_required(VERSION 3.25)
    project(htmlstats)
    
    add_executable(htmlstats
        src/main.cpp
    )
    
    target_compile_features(htmlstats PRIVATE cxx_std_20)
    target_link_libraries(htmlstats PRIVATE erbsland-re)
    

This file focuses exclusively on your application target:

  • Defines an executable target The htmlstats binary is built using src/main.cpp as its entry point.

  • Enables the C++20 standard Using target_compile_features keeps the requirement local to this target and plays nicely with larger projects.

  • Links against the Erbsland RE library The library target erbsland-re is provided by the submodule’s own CMake configuration and is now available automatically.

This target-based approach is modern CMake best practice and scales well as your build grows more complex.

The Current Project State

At this point, your project directory structure should look like this. Newly added files are highlighted:

htmlstats
    ├── erbsland-cpp-re
    ├── htmlstats
    │   ├── src
    │   └── CMakeLists.txt           # [new] Application CMake file
    └── CMakeLists.txt               # [new] Top-level CMake file

Your build system is now fully wired up: CMake knows about the external library, your application, and how the two fit together.

Write the Main Functionality →