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()
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, orapp.exeon Windows) unless you setOUTPUT_NAME.WIN32makes a GUI program on Windows (WinMainentry point, no console);MACOSX_BUNDLEmakes an.appbundle on macOS. Both are ignored elsewhere.EXCLUDE_FROM_ALLleaves 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:
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.hppis 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 needtarget_include_directories(next lessons).OUTPUT_NAMEchanged the file name totad-calc, while the target is still calledcalcinside CMake.scratchwas not built by the plaincmake --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:
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 librariesOn 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) |
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?
- The target is never built
- It is left out of the default
allbuild; it builds when requested or needed by another target - It excludes it from installation only
- It hides it from IDEs
You want the file to be called tad-calc but keep the target name calc. What do you set?
RUNTIME_OUTPUT_NAMEon the project- The
OUTPUT_NAMEtarget property project(tad-calc)CMAKE_EXECUTABLE_SUFFIX
Why might you set CMAKE_RUNTIME_OUTPUT_DIRECTORY on Windows?
- To speed up compiling
- So executables and DLLs land in the same folder and the DLLs are found at run time
- It is required by MSVC
- To enable installation