TExcelTAD
Home › Modern CMake (with CTest & CPack) › Executables & Sources

Executables & Sources

A target is anything CMake builds: an executable, a library, or a custom step. Targets are the unit of modern CMake: everything you configure (sources, include paths, flags, dependencies) is attached to a target, not set globally.

add_executable()

cmake
add_executable(<name> [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [source1 ...])
  • <name> is the target name, unique in the whole build. The file is named after it (app, or app.exe on Windows) unless you set OUTPUT_NAME.
  • WIN32 makes a GUI program on Windows (WinMain entry point, no console); MACOSX_BUNDLE makes an .app bundle on macOS. Both are ignored elsewhere.
  • EXCLUDE_FROM_ALL leaves the target out of the default build; it is built only when asked for (--target) or when something depends on it.

Adding sources later: target_sources()

Sources can be listed in add_executable, or added afterwards with target_sources, which is handy when a subdirectory or an if() contributes files:

bash · try it yourself
mkdir -p calc/src && cd calc
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Calc LANGUAGES CXX)

add_executable(calc)                       # no sources yet
target_sources(calc PRIVATE src/main.cpp src/ops.cpp src/ops.hpp)

option(CALC_WITH_HISTORY "Keep a history of results" ON)
if(CALC_WITH_HISTORY)
  target_sources(calc PRIVATE src/history.cpp)
  target_compile_definitions(calc PRIVATE CALC_WITH_HISTORY)
endif()

set_target_properties(calc PROPERTIES OUTPUT_NAME "tad-calc")
add_executable(scratch EXCLUDE_FROM_ALL src/scratch.cpp)
CM
cat > src/ops.hpp <<'SRC'
#pragma once
int add(int a, int b);
SRC
cat > src/ops.cpp <<'SRC'
#include "ops.hpp"
int add(int a, int b) { return a + b; }
SRC
cat > src/history.cpp <<'SRC'
#include <cstdio>
void remember(int v) { std::printf("history: %d\n", v); }
SRC
cat > src/main.cpp <<'SRC'
#include <cstdio>
#include "ops.hpp"
void remember(int);
int main() {
    int r = add(2, 3);
    std::printf("2 + 3 = %d\n", r);
#ifdef CALC_WITH_HISTORY
    remember(r);
#endif
}
SRC
echo 'int main() { return 0; }' > src/scratch.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build
./build/tad-calc
echo "--- scratch is only built on request"
cmake --build build --target scratch
[1/4] Building CXX object CMakeFiles/calc.dir/src/main.cpp.o
[2/4] Building CXX object CMakeFiles/calc.dir/src/ops.cpp.o
[3/4] Building CXX object CMakeFiles/calc.dir/src/history.cpp.o
[4/4] Linking CXX executable tad-calc
2 + 3 = 5
history: 5
--- scratch is only built on request
[1/2] Building CXX object CMakeFiles/scratch.dir/src/scratch.cpp.o
[2/2] Linking CXX executable scratch

Things to notice:

  • Headers can be listed as sources. src/ops.hpp is not compiled, but listing it makes IDEs show it as part of the target. It is optional.
  • #include "ops.hpp" found the header because quoted includes search the including file's own directory first. Headers in other directories need target_include_directories (next lessons).
  • OUTPUT_NAME changed the file name to tad-calc, while the target is still called calc inside CMake.
  • scratch was not built by the plain cmake --build build.

Where the files go

By default an executable lands in the build directory that corresponds to the CMakeLists.txt that created it (build/ here, build/tools/ for a target in tools/CMakeLists.txt). To collect all executables in one folder, set the output directory before creating targets:

cmake
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)   # executables (and DLLs on Windows)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)   # shared libraries
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)   # static libraries

On Windows this matters: a program can only find its DLLs at run time if they sit next to it (or on PATH), so putting executables and DLLs in the same bin/ folder saves a lot of "DLL not found" errors.

Building a target by name

cmake --build build --target <name> builds one target and whatever it depends on. A few target names exist in every project:

Target Meaning
all the default: everything not marked EXCLUDE_FROM_ALL
clean delete build outputs
install build, then run the install rules (prefer cmake --install)
help list the primary targets (cmake --build build --target help)
bash · try it yourself
mkdir -p two && cd two
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Two LANGUAGES CXX)
add_executable(server server.cpp)
add_executable(client client.cpp)
CM
echo 'int main() { return 0; }' > server.cpp
cp server.cpp client.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build --target client
[1/2] Building CXX object CMakeFiles/client.dir/client.cpp.o
[2/2] Linking CXX executable client

Test yourself

What does EXCLUDE_FROM_ALL on add_executable do?

  1. The target is never built
  2. It is left out of the default all build; it builds when requested or needed by another target
  3. It excludes it from installation only
  4. It hides it from IDEs

You want the file to be called tad-calc but keep the target name calc. What do you set?

  1. RUNTIME_OUTPUT_NAME on the project
  2. The OUTPUT_NAME target property
  3. project(tad-calc)
  4. CMAKE_EXECUTABLE_SUFFIX

Why might you set CMAKE_RUNTIME_OUTPUT_DIRECTORY on Windows?

  1. To speed up compiling
  2. So executables and DLLs land in the same folder and the DLLs are found at run time
  3. It is required by MSVC
  4. To enable installation