Toolchain Files
A toolchain file is a small CMake script that describes which compiler and tools to use and, for cross-compiling, which system you are building for. It is read at the very beginning of the first configure, before project() detects the compiler, and it keeps that information out of your CMakeLists.txt, where it doesn't belong: the same project builds with GCC, Clang, MSVC or a cross compiler depending only on the toolchain file you pass.
cmake -S . -B build --toolchain clang.cmake # CMake 3.21+
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=clang.cmake # older spelling, same effectSwitching to Clang
The simplest toolchain file just picks compilers. This session builds the same program with the default GCC and with a Clang toolchain file, side by side:
mkdir -p proj && cd proj
cat > clang.cmake <<'CM'
# Toolchain: system Clang with LLVM's lld linker
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_LINKER_TYPE LLD) # CMake 3.29+: -fuse-ld=lld
CM
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.29)
project(Which LANGUAGES CXX)
message("compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION} (${CMAKE_CXX_COMPILER})")
add_executable(which main.cpp)
CM
cat > main.cpp <<'SRC'
#include <cstdio>
int main() {
#if defined(__clang__)
std::printf("built by clang %d.%d\n", __clang_major__, __clang_minor__);
#else
std::printf("built by gcc %d.%d\n", __GNUC__, __GNUC_MINOR__);
#endif
}
SRC
cmake -S . -B build-gcc -G Ninja --log-level=NOTICE 2>&1 | grep '^compiler'
cmake -S . -B build-clang -G Ninja --log-level=NOTICE --toolchain clang.cmake 2>&1 | grep '^compiler'
cmake --build build-gcc > /dev/null && ./build-gcc/which
cmake --build build-clang -v | grep -o 'clang++ .*-fuse-ld=lld.*-o which'
./build-clang/which
grep '^CMAKE_TOOLCHAIN_FILE:' build-clang/CMakeCache.txtcompiler: GNU 13.3.0 (/usr/bin/c++)
compiler: Clang 18.1.3 (/usr/bin/clang++)
built by gcc 13.3
clang++ -fuse-ld=lld -Xlinker --dependency-file=CMakeFiles/which.dir/link.d CMakeFiles/which.dir/main.cpp.o -o which
built by clang 18.1
CMAKE_TOOLCHAIN_FILE:FILEPATH=/home/learner/proj/clang.cmake
The toolchain file's path is recorded in the cache (CMAKE_TOOLCHAIN_FILE), so later re-configures of build-clang use it automatically.
Note: For a one-off switch you can also set the
CCandCXXenvironment variables, or pass-DCMAKE_CXX_COMPILER=clang++, on the first configure. A toolchain file is better when there is more than one setting, and it's the only sane way to cross-compile.
The compiler is fixed per build directory
The compiler is detected once. Changing it in an existing build directory doesn't work the way people expect:
mkdir -p proj && cd proj
printf 'cmake_minimum_required(VERSION 3.25)\nproject(P LANGUAGES CXX)\n' > CMakeLists.txt
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++ 2>&1 | grep -v '^--' | sed -n '1,6p'You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_CXX_COMPILER= clang++
CMake noticed the change, threw away the entire cache (including every option you had set) and configured from scratch. Use a separate build directory per toolchain instead.
What a real cross toolchain file contains
For cross-compiling, the toolchain file also tells CMake about the target system and where its libraries and headers live (the sysroot). Here is a typical file for 64-bit ARM Linux with a GNU cross toolchain (gcc-aarch64-linux-gnu on Debian/Ubuntu):
# aarch64-linux-gnu.cmake
set(CMAKE_SYSTEM_NAME Linux) # the TARGET OS; setting it turns on cross-compiling mode
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_SYSROOT /opt/sysroots/aarch64) # headers + libraries of the target system
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
# find_package / find_library / find_path: search the sysroot, never the build machine...
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT} /opt/aarch64-deps)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
# ...but programs (code generators, protoc) must run on the build machine
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Optional: run target binaries (tests, try_run) through an emulator
set(CMAKE_CROSSCOMPILING_EMULATOR qemu-aarch64;-L;${CMAKE_SYSROOT})| Variable | Why |
|---|---|
CMAKE_SYSTEM_NAME |
the target OS (Linux, Windows, Android, iOS, Generic for bare metal); setting it makes CMAKE_CROSSCOMPILING true |
CMAKE_SYSTEM_PROCESSOR |
the target CPU, used by your project and by some modules |
CMAKE_SYSROOT |
passed to the compiler as --sysroot, and used as a find root |
CMAKE_<LANG>_COMPILER |
the cross compilers (or Clang plus CMAKE_<LANG>_COMPILER_TARGET) |
CMAKE_FIND_ROOT_PATH_MODE_* |
keep find_* from picking up the build machine's x86-64 libraries |
CMAKE_CROSSCOMPILING_EMULATOR |
lets add_test and custom commands run target binaries |
CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY |
for bare-metal targets that can't link an executable without a linker script |
What doesn't belong in a toolchain file: project options, warnings, build types, dependencies. Toolchain files are shared between projects; keep them about the toolchain. Android's NDK, Emscripten and vcpkg all ship toolchain files you pass the same way ($NDK/build/cmake/android.toolchain.cmake, Emscripten.cmake, scripts/buildsystems/vcpkg.cmake).
The next lesson cross-compiles for real.
Test yourself
When is a toolchain file read?
- After project()
- At the start of the first configure, before project() detects the compiler
- At build time
- At install time
You pass -DCMAKE_CXX_COMPILER=clang++ to an existing GCC build directory. What happens?
- Only new files use clang
- CMake detects the change, deletes the whole cache and configures from scratch
- It's ignored
- Both compilers are used
Why set CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER in a cross toolchain?
- Programs are never needed
- Tools such as code generators must be found on the build machine, since they run there, while libraries come from the sysroot
- It speeds up configure
- It disables the emulator