TExcelTAD
Home › Modern CMake (with CTest & CPack) › Configure, Build, Run

Configure, Build, Run

This lesson is about the command line: how to configure, build, rebuild and clean, and what CMake leaves in the build directory.

Out-of-source builds

CMake keeps generated files out of your source tree. -S is the source directory, -B the build directory, which CMake creates if needed:

bash
cmake -S . -B build                  # configure + generate
cmake --build build                  # build everything
cmake --build build --target hello   # build one target
cmake --build build --clean-first    # clean, then build
cmake --build build -v               # show the full compiler command lines
cmake --build build -j 8             # parallel jobs (passed to Ninja/Make)

cmake --build is a portable wrapper: it runs ninja, make or MSBuild for you, so scripts and CI don't need to know which generator was used. Everything after -- is passed to the native tool unchanged (cmake --build build -- -d explain for Ninja).

A full cycle

This session configures a two-file project, builds it, shows the build directory, then changes a source file and rebuilds: only the changed file is recompiled. Finally it changes CMakeLists.txt, and the build step re-runs CMake automatically.

bash · try it yourself
mkdir -p tool && cd tool
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Tool LANGUAGES CXX)
add_executable(tool main.cpp util.cpp)
CM
cat > util.cpp <<'SRC'
int answer() { return 42; }
SRC
cat > main.cpp <<'SRC'
#include <cstdio>
int answer();
int main() { std::printf("answer = %d\n", answer()); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE
cmake --build build
./build/tool
ls build
echo "--- nothing changed"
cmake --build build
echo "--- util.cpp changed"
sleep 1 && echo 'int answer() { return 7; }' > util.cpp
cmake --build build && ./build/tool
echo "--- CMakeLists.txt changed"
sleep 1 && echo 'message("re-configured")' >> CMakeLists.txt
cmake --build build
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/tool/build
[1/3] Building CXX object CMakeFiles/tool.dir/main.cpp.o
[2/3] Building CXX object CMakeFiles/tool.dir/util.cpp.o
[3/3] Linking CXX executable tool
answer = 42
CMakeCache.txt
CMakeFiles
build.ninja
cmake_install.cmake
tool
--- nothing changed
ninja: no work to do.
--- util.cpp changed
[1/2] Building CXX object CMakeFiles/tool.dir/util.cpp.o
[2/2] Linking CXX executable tool
answer = 7
--- CMakeLists.txt changed
[0/1] Re-running CMake...
re-configured
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/learner/tool/build
ninja: no work to do.

What is in the build directory:

File Purpose
CMakeCache.txt every cache variable: the compiler found, options you set with -D, results of find_package. Delete it (or use cmake --fresh) to start the configuration over
CMakeFiles/ CMake's internal files: compiler detection results, per-target object directories (tool.dir/main.cpp.o)
build.ninja the generated build file (Makefile with the Makefile generator)
cmake_install.cmake the script cmake --install runs

Verbose builds

When a build fails in a confusing way, look at the actual compiler command. -v (or --verbose) shows it:

bash · try it yourself
mkdir -p tool && cd tool
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Tool LANGUAGES CXX)
add_executable(tool main.cpp)
target_compile_definitions(tool PRIVATE GREETING="hi")
CM
echo 'int main() {}' > main.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build -v
Change Dir: '/home/learner/tool/build'

Run Build Command(s): /usr/local/bin/ninja -v -j 1
[1/2] /usr/bin/c++ -DGREETING=\"hi\"  -fno-diagnostics-color -MD -MT CMakeFiles/tool.dir/main.cpp.o -MF CMakeFiles/tool.dir/main.cpp.o.d -o CMakeFiles/tool.dir/main.cpp.o -c /home/learner/tool/main.cpp
[2/2] : && /usr/bin/c++  -Wl,--dependency-file=CMakeFiles/tool.dir/link.d CMakeFiles/tool.dir/main.cpp.o -o tool   && :

The command line shows a few things CMake adds on its own: -MD -MT … -MF … makes GCC write a header-dependency file so Ninja knows to rebuild main.cpp.o when an included header changes, -fno-diagnostics-color comes from CMAKE_COLOR_DIAGNOSTICS=OFF, and the definition you asked for appears as -DGREETING=\"hi\".

Starting over

There are three levels of "start again":

Command Effect
cmake --build build --target clean delete build outputs, keep the configuration
cmake --fresh -S . -B build discard CMakeCache.txt and re-configure from scratch (CMake 3.24+)
rm -rf build delete everything; always safe, because nothing in a build directory is precious

The cache matters: a variable set with -D on the first configure stays set on later runs, even if you don't pass it again. --fresh (or deleting the directory) is how you get rid of it. The Options & Cache Variables lesson goes deeper.

bash · try it yourself
mkdir -p tool && cd tool
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Tool LANGUAGES NONE)
message("MODE is '${MODE}'")
CM
cmake -S . -B build -DMODE=fast --log-level=NOTICE 2>&1 | grep -v "^--"
cmake -S . -B build --log-level=NOTICE 2>&1 | grep -v "^--"
cmake --fresh -S . -B build --log-level=NOTICE 2>&1 | grep -v "^--"
MODE is 'fast'
MODE is 'fast'
MODE is ''

LANGUAGES NONE is a handy trick for experiments: no compiler detection at all.

Running from the build directory

You will also see the older style mkdir build && cd build && cmake ... It does the same thing (the current directory is the build directory, .. the source directory). -S/-B is clearer and works from anywhere, so this course uses it.

Test yourself

You edited CMakeLists.txt. What do you need to run?

  1. cmake -S . -B build again, then cmake --build build
  2. Only cmake --build build: it re-runs CMake automatically when a CMakeLists.txt changed
  3. rm -rf build first
  4. cmake --install build

How do you see the exact compiler command lines of a build?

  1. cmake --build build -v
  2. cmake --trace
  3. cmake --build build --debug
  4. ninja -t clean

You configured once with -DMODE=fast. On the next cmake -S . -B build without -D, what is MODE?

  1. Empty
  2. fast: it is stored in CMakeCache.txt
  3. An error
  4. OFF