Add Targets for lots of Small Executables

Introduction

add_executables(PATH [PATH ...] [LIBRARIES <lib1> <lib2> ...]) processes source files found in the provided path arguments and generates corresponding executable targets.

The path arguments can be directories or individual source files. If a directory is provided, the function will process all the source files found in the directory and its subdirectories. Source files are assumed to have a supported C/C++ extension (.c, .cc, .cxx, .cpp, .C, .c++).

To use this function, we expect each source file found in the provided paths to generate a standalone executable program (so Foo.cpp will generate an executable called Foo).

For example, if we pass a directory that has a file Foo.cpp, the function will create a target Foo that compiles Foo.cpp and links the object file to the libraries from the LIBRARIES argument.

Suppose the project looks like:

project/
├── .git
├── CMakeLists.txt
├── cmake
│   └── add_executables.cmake
├── examples
│   ├── Bar01.cpp
│   ├── Bar02.cpp
│   ├── Bar03.cpp
│   ├── Foo01.cpp
│   └── Foo02.cpp
└── ...

and your CMakeLists.txt file has some lines like

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(add_executables)
add_executables(examples LIBRARIES nlohmann_json::nlohmann_json)

then CMake will create individual targets for Bar01, Bar02, Bar03, Foo01, and Foo02 and link them to the library nlohmann_json.

Rationale

Library developers often develop an extensive set of simple command-line tools that demonstrate or test various features of their library. This is how one ends up with a directory like the examples above.

The add_executables function makes it easy to grow that collection of executables without worrying about adding them to some CMakeLists.txt file.

Back to top