CodeBlanks Logo Icon CodeBlanks

Summary - Build systems

Combine CMake, find_package and multi-project to set up a project structure.

Summary: setting up a CMake project

In this summary you will combine CMake basics, find_package and multi-project structure. You will set up a root CMakeLists.txt with target definitions in two subdirectories (lib and app). This is the skeleton of real C++ projects.

The root file contains cmake_minimum_required, project and two add_subdirectory calls. find_package finds an external library. The application target links to the library. This structure is both modular and reusable; adding a new subdirectory needs only one add_subdirectory line.

Practice Write a complete CMake project

Write the root CMakeLists.txt. Add cmake_minimum_required(VERSION 3.20) and project(MyApp). Find the threading library with find_package(Threads REQUIRED). Include two subdirectories with add_subdirectory(lib) and add_subdirectory(app).

Starter code

main.cpp
# CMakeLists.txt (root)
# Add all commands here

Sample solution

main.cpp
cmake_minimum_required(VERSION 3.20)
project(MyApp)
find_package(Threads REQUIRED)
add_subdirectory(lib)
add_subdirectory(app)

Expected output

Project ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does a multi-project CMake structure provide?

  • Faster compilation
  • Colored output
  • Less memory usage
  • Modular and reusable structure Correct answer

Explanation: The multi-project layout keeps the library separate and reusable.

2 Complete the add_subdirectory command

Question
____(app)
Answer
add_subdirectory(app)

Explanation: add_subdirectory(app) brings the app directory into the build.

3 Match the command to its role

Term Meaning
cmake_minimum_required Sets version
find_package Finds a library
add_subdirectory Adds a subdirectory
add_executable Defines an executable

All lessons in Advanced