In CMake, how do CMAKE_CXX_STANDARD, CMAKE_CXX_STANDARD_REQUIRED and target_compile_features differ when a project needs C++20?
answer
- property default vs usage requirement
- decay without REQUIRED
- gnu++20 vs c++20
- cxx_std_20 propagates
- highest requirement wins
basics
~20 sCMAKE_CXX_STANDARD sets the default CXX_STANDARD of targets created afterwards; without CMAKE_CXX_STANDARD_REQUIRED it may decay to an older standard. target_compile_features(lib PUBLIC cxx_std_20) is a per-target minimum that also propagates to consumers and fails if the compiler cannot meet it.
solid answer
~40 s`set(CMAKE_CXX_STANDARD 20)` initialises the `CXX_STANDARD` property of every target created after it in that scope. On its own it is a request: if the compiler has no flag for C++20, CMake **decays** to an earlier standard's flag without an error, unless `CMAKE_CXX_STANDARD_REQUIRED ON` makes it a requirement. `CMAKE_CXX_EXTENSIONS` (default `ON`) picks `-std=gnu++20` over `-std=c++20`. `target_compile_features(core PUBLIC cxx_std_20)` states a **minimum** for one target; as a `PUBLIC` or `INTERFACE` usage requirement it also reaches every consumer, and it is enforced. When several requirements meet, CMake uses the highest: a target with `CMAKE_CXX_STANDARD 17` that links a library requiring `cxx_std_20` compiles with C++20. The common pattern is the variables in the top-level project for its own code and `target_compile_features` on libraries that others consume.
code
cmake · 9 linescmake_minimum_required(VERSION 3.25)
project(render CXX)
set(CMAKE_CXX_STANDARD 17)
add_library(core STATIC core.cpp)
target_compile_features(core PUBLIC cxx_std_20)
add_executable(render main.cpp)
target_link_libraries(render PRIVATE core)go deeper
Know that set(CMAKE_CXX_STANDARD 20) before creating targets selects the standard, and that hand-written -std flags are the wrong tool.
Explain decay without REQUIRED, the gnu++ default from CXX_EXTENSIONS, and why target_compile_features PUBLIC propagates to consumers.
Read the real -std flag on compile lines when behaviour differs between machines, and put the standard on library targets as a usage requirement.
Set the team's standard level and extensions policy once at the top level, and require libraries to publish their minimum through compile features.
## Two mechanisms, one flag CMake chooses one language-standard flag per target, such as `-std=gnu++20` for GCC and Clang. Two mechanisms feed that choice: - **Target properties** `CXX_STANDARD`, `CXX_STANDARD_REQUIRED` and `CXX_EXTENSIONS`, usually initialised from the variables `CMAKE_CXX_STANDARD`, `CMAKE_CXX_STANDARD_REQUIRED` and `CMAKE_CXX_EXTENSIONS`. - **Compile features** requested with `target_compile_features`, most often the **meta-features** `cxx_std_11` through `cxx_std_26`, which mean "a compiler mode at least this new". ## The variables are defaults for new targets `set(CMAKE_CXX_STANDARD 20)` does not change existing targets. It sets the initial value of `CXX_STANDARD` for each target created **after** it in the same directory scope or below: - Put it near the top of the top-level `CMakeLists.txt`, before `add_library` and `add_executable`. - A target created before the `set()` keeps no standard requirement from it. - Supported values are `98`, `11`, `14`, `17`, `20`, `23` (CMake 3.20+) and `26` (CMake 3.25+). ## REQUIRED and decay Without `CXX_STANDARD_REQUIRED`, the standard is optional. The `CXX_STANDARD` documentation spells out the **decay** behaviour: if the requested level has no flag for the compiler in use, CMake adds the previous standard's flag instead, with no error or warning. A team that sets `CMAKE_CXX_STANDARD 20` on an old compiler can therefore build in C++17 mode and only discover it when a C++20 feature fails to compile. `set(CMAKE_CXX_STANDARD_REQUIRED ON)` turns the request into a requirement, so configuring fails clearly instead. ## Extensions `CXX_EXTENSIONS` selects between the standard mode and the compiler's extended mode. It is `ON` by default, which is why CMake emits `-std=gnu++20` rather than `-std=c++20` for GCC and Clang. Setting `CMAKE_CXX_EXTENSIONS OFF` gives the strict `-std=c++20` spelling, which many teams prefer so that code does not quietly rely on GNU extensions. Under policy `CMP0128` (CMake 3.22+) the default comes from the compiler's detected default, and CMake avoids adding a flag when the compiler's own default already satisfies the request. ## target_compile_features: a requirement that travels ```cmake add_library(core STATIC core.cpp) target_compile_features(core PUBLIC cxx_std_20) add_executable(render main.cpp) target_link_libraries(render PRIVATE core) ``` `cxx_std_20` with `PUBLIC` is a **usage requirement**: it applies to `core` and to anything linking `core`. Because a header that uses C++20 syntax forces every includer to compile as C++20, this is the right tool for libraries. It is always a requirement: an unsupported meta-feature is an error, not a decay. ## When requirements meet CMake picks the highest level any source of requirements asks for. In a CMake 4.4.4 run with `set(CMAKE_CXX_STANDARD 17)` at the top and the snippet above, both `core.cpp` and `main.cpp` compiled with `-std=gnu++20`: `render`'s own property asked for 17, but the requirement it inherited from `core` asked for 20. A higher `CXX_STANDARD`, by contrast, is not lowered by a smaller meta-feature. | Mechanism | Scope | Enforced | Propagates to consumers | |---|---|---|---| | `CMAKE_CXX_STANDARD` | targets created later | only with `CMAKE_CXX_STANDARD_REQUIRED ON` | no | | `CXX_STANDARD` property | one target | only with `CXX_STANDARD_REQUIRED ON` | no | | `target_compile_features(... PUBLIC cxx_std_20)` | one target | yes | yes, as a usage requirement | ## Checking what you got Standard-mode surprises usually show up as "works on my machine": one compiler's default happens to be new enough, another's is not. To see what CMake decided: - `cmake --build build -v` shows the `-std=` flag on each compile line, or its absence when the compiler's default already satisfies the request. - `CMAKE_CXX_COMPILE_FEATURES` lists the features and meta-features CMake knows the compiler supports; a `cxx_std_23` missing from it means a `cxx_std_23` requirement will fail. - `CMAKE_CXX_STANDARD_DEFAULT` holds the standard CMake detected as the compiler's own default. - The `CXX_STANDARD` property of a target, read with `get_target_property`, shows only what that target asked for, not what it inherited; the compile line is the final word. A mismatch between two developers' builds is usually decay (no `REQUIRED`), a different compiler default under `CMP0128`, or a library that forgot to publish its requirement with `target_compile_features`. ## A sensible combination 1. In the top-level project: `set(CMAKE_CXX_STANDARD 20)`, `set(CMAKE_CXX_STANDARD_REQUIRED ON)` and usually `set(CMAKE_CXX_EXTENSIONS OFF)`. 2. On each library whose public headers need a standard: `target_compile_features(lib PUBLIC cxx_std_20)`, so consumers in other projects get the right mode even if they never set the variables. 3. Never add `-std=c++20` with `target_compile_options` or `CMAKE_CXX_FLAGS`; it bypasses CMake's bookkeeping and can clash with the flag CMake adds itself. Which language features C++20 actually brings belongs to the material on standard versions.
- Why does CMake pass -std=gnu++20 instead of -std=c++20, and how do you change that?The `CXX_EXTENSIONS` property is `ON` by default, so CMake asks GCC and Clang for the GNU dialect. Set `CMAKE_CXX_EXTENSIONS OFF` before creating targets, or the `CXX_EXTENSIONS` property on one target, and CMake emits `-std=c++20`. Under policy CMP0128 the default comes from the compiler's detected default, which for GCC and Clang is the GNU dialect.
- A library sets CMAKE_CXX_STANDARD 20 in its own CMakeLists.txt; why might a consumer still compile its headers as C++17?The variable only sets `CXX_STANDARD` on the library's own targets and is not a usage requirement, so nothing reaches a consumer's targets through `target_link_libraries`. If the library's headers need C++20, it must say so with `target_compile_features(lib PUBLIC cxx_std_20)`, which travels with the target, including through an installed export.
- Setting CMAKE_CXX_STANDARD after add_library still applies to that library
- CMAKE_CXX_STANDARD 20 guarantees a C++20 build or an error
- target_compile_features with cxx_std_20 can lower a target that asked for C++23
- Adding -std=c++20 to CMAKE_CXX_FLAGS is equivalent to setting the standard properly
- CMAKE_CXX_STANDARD set in a library's project reaches the projects that link it