TExcelTAD
Home › Modern CMake (with CTest & CPack) › Checking the Platform

Checking the Platform

Portable C and C++ code sometimes has to ask questions before it compiles: does this system have <sys/epoll.h>? Is strlcpy available? Does the compiler accept -fstack-protector-strong? Autotools answered these with ./configure; CMake has a family of check modules that compile tiny test programs during configure and store the answer in a cache variable.

Module Command Question
CheckIncludeFileCXX / CheckIncludeFile check_include_file_cxx(<header> <var>) does the header exist?
CheckSymbolExists check_symbol_exists(<symbol> "<headers>" <var>) is the function or macro declared in those headers?
CheckCXXSourceCompiles check_cxx_source_compiles("<code>" <var>) does this snippet compile and link?
CheckCXXCompilerFlag check_cxx_compiler_flag(<flag> <var>) does the compiler accept this flag?
CheckTypeSize check_type_size(<type> <var>) how big is this type?
CheckIPOSupported check_ipo_supported(RESULT <var>) does link-time optimisation work?

(There are also CheckSourceCompiles and CheckCompilerFlag, language-generic versions that take the language as the first argument.)

Checks in action

bash · try it yourself
mkdir -p plat && cd plat
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Plat LANGUAGES C CXX)
include(CheckIncludeFileCXX)
include(CheckSymbolExists)
include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)
include(CheckTypeSize)
include(CheckIPOSupported)

check_include_file_cxx(sys/epoll.h HAVE_EPOLL)
check_include_file_cxx(sys/event.h HAVE_KQUEUE)
check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY)
check_symbol_exists(memfd_create "sys/mman.h" HAVE_MEMFD_CREATE)
check_cxx_source_compiles("
  #include <expected>
  int main() { std::expected<int, int> e = 1; return *e - 1; }" HAVE_STD_EXPECTED)
check_cxx_compiler_flag(-fstack-protector-strong HAVE_STACK_PROTECTOR)
check_cxx_compiler_flag(-fno-such-flag HAVE_NO_SUCH_FLAG)
check_type_size("long" SIZEOF_LONG LANGUAGE CXX)
check_ipo_supported(RESULT HAVE_IPO LANGUAGES CXX)
message("system: ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_PROCESSOR}, ipo: ${HAVE_IPO}")
CM
cmake -S . -B build -G Ninja 2>&1 | grep -E '^-- (Looking|Performing|Check size)|^system'
echo "--- second configure: cached, no tests run"
cmake -S . -B build 2>&1 | grep -cE '^-- (Looking|Performing)'
-- Looking for C++ include sys/epoll.h
-- Looking for C++ include sys/epoll.h - found
-- Looking for C++ include sys/event.h
-- Looking for C++ include sys/event.h - not found
-- Looking for strlcpy
-- Looking for strlcpy - found
-- Looking for memfd_create
-- Looking for memfd_create - not found
-- Performing Test HAVE_STD_EXPECTED
-- Performing Test HAVE_STD_EXPECTED - Failed
-- Performing Test HAVE_STACK_PROTECTOR
-- Performing Test HAVE_STACK_PROTECTOR - Success
-- Performing Test HAVE_NO_SUCH_FLAG
-- Performing Test HAVE_NO_SUCH_FLAG - Failed
-- Looking for C++ include sys/types.h
-- Looking for C++ include sys/types.h - found
-- Looking for C++ include stdint.h
-- Looking for C++ include stdint.h - found
-- Looking for C++ include stddef.h
-- Looking for C++ include stddef.h - found
-- Check size of long
-- Check size of long - done
system: Linux x86_64, ipo: YES
--- second configure: cached, no tests run
0

A few observations:

  • strlcpy was added to glibc in 2.38; Ubuntu 24.04 has glibc 2.39, so the check finds it. On an older system the same project falls back cleanly.
  • memfd_create exists in glibc, yet the check says not found: <sys/mman.h> declares it only when _GNU_SOURCE is defined. Checks compile exactly what you tell them; set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) before the check (and the same definition on the target) fixes it.
  • std::expected needs C++23; GCC 13 defaults to C++17, so the snippet doesn't compile. Checks use the flags in CMAKE_REQUIRED_FLAGS, CMAKE_REQUIRED_DEFINITIONS, CMAKE_REQUIRED_INCLUDES and CMAKE_REQUIRED_LIBRARIES; set CMAKE_CXX_STANDARD 23 (or CMAKE_REQUIRED_FLAGS -std=c++23) before the check if that's what you'll compile with.
  • check_type_size first ran three header checks of its own (sys/types.h, stdint.h, stddef.h).
  • The second configure ran no tests: the results are cache variables. After changing the compiler or the flags, use --fresh (or -U HAVE_*), or the old answers stay.

Using the answers

Feed results into a generated header (configure_file) or into a target:

cmake
configure_file(config.h.in generated/config.h)        # #cmakedefine HAVE_EPOLL ...

if(HAVE_EPOLL)
  target_sources(net PRIVATE src/poller_epoll.cpp)
elseif(HAVE_KQUEUE)
  target_sources(net PRIVATE src/poller_kqueue.cpp)
else()
  target_sources(net PRIVATE src/poller_select.cpp)
endif()

if(HAVE_STACK_PROTECTOR)
  target_compile_options(net PRIVATE -fstack-protector-strong)
endif()

if(HAVE_IPO)
  set_property(TARGET net PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)
endif()

Platform variables

For broad decisions, CMake already knows the platform:

Variable Example values
CMAKE_SYSTEM_NAME Linux, Windows, Darwin, Android, iOS, Emscripten (the target system)
CMAKE_HOST_SYSTEM_NAME the machine running CMake (differs when cross-compiling)
CMAKE_SYSTEM_PROCESSOR x86_64, aarch64, AMD64, arm64
WIN32, APPLE, UNIX, LINUX (3.25+), MSVC shortcuts, true when they apply
CMAKE_CXX_COMPILER_ID GNU, Clang, AppleClang, MSVC
CMAKE_SIZEOF_VOID_P 8 on 64-bit targets

Prefer feature checks over platform checks when you can: "has epoll" is more accurate than "is Linux", and keeps working on the next platform nobody thought of.

Test yourself

Why does a second configure skip the checks?

  1. They are disabled after the first run
  2. Each result is stored in a cache variable; only --fresh or -U removes it
  3. Ninja caches them
  4. Checks only run in Debug

Which module answers 'does this code snippet compile and link?'

  1. CheckIncludeFileCXX
  2. CheckSymbolExists
  3. CheckCXXSourceCompiles
  4. CheckTypeSize

When cross-compiling for Android on Linux, what is CMAKE_SYSTEM_NAME?

  1. Linux
  2. Android (the target system); CMAKE_HOST_SYSTEM_NAME is Linux
  3. Both
  4. Unset