In CMake, how do you add warning flags for GCC and Clang only with a generator expression, and which mistakes make it silently fail?
answer
- test the compiler id
- Apple clang has its own id
- semicolon, not space, between flags
- quote the whole expression
basics
~10 sWrap the flags in a compiler-id test: "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall;-Wextra>". Forgetting AppleClang skips Apple clang, and a space inside the quoted expression passes "-Wall -Wextra" as one bogus flag.
solid answer
~40 s`$<CXX_COMPILER_ID:ids>` yields `1` when CMake's id for the C++ compiler is in the comma-separated list (lists need CMake 3.15+), and `$<cond:text>` keeps `text` only then: `target_compile_options(imgproc PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall;-Wextra>")`. Three traps make it fail without an error. **Apple clang reports `AppleClang`**, not `Clang`; an Apple clang 21 run evaluated `$<CXX_COMPILER_ID:Clang>` to `0`. **Separate flags with `;`**: a quoted space produced the single argument `"-Wall -Wextra"`, which clang rejected as an unknown warning option. **Quote the expression**, because the manual warns that unquoted spaces can split it so it is no longer recognized. Since CMake 4.0 the old policy that reported Apple clang as `Clang` can no longer be requested, so `AppleClang` is the only id.
code
cmake · 11 linesadd_library(imgproc STATIC blur.cpp)
set(gcc_like "$<CXX_COMPILER_ID:GNU,Clang,AppleClang>")
set(msvc "$<CXX_COMPILER_ID:MSVC>")
target_compile_options(imgproc PRIVATE
"$<${gcc_like}:-Wall;-Wextra>" # two flags: semicolon-separated
"$<${msvc}:/W4>")
# Wrong: one argument "-Wall -Wextra"
# target_compile_options(imgproc PRIVATE "$<${gcc_like}:-Wall -Wextra>")go deeper
Recall the $<$<CXX_COMPILER_ID:...>:flags> shape and that the inner test yields 1 or 0.
Explain id lists, the AppleClang id, why flags are separated by semicolons and why the whole expression is quoted.
Diagnose warnings that silently vanish on one platform by dumping the compiler id and the real compile line, and prefer COMPILE_LANG_AND_ID on mixed-language targets.
Centralize the team's warning policy in one named interface target or helper so compiler-specific expressions live in one reviewed place.
## The pattern A warning set is a classic per-compiler setting: GCC and Clang accept `-Wall -Wextra`, while MSVC uses `/W4`. CMake knows which compiler it found and exposes it as a **compiler id**, which generator expressions can test while the build system is generated: ```cmake target_compile_options(imgproc PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall;-Wextra>" "$<$<CXX_COMPILER_ID:MSVC>:/W4>") ``` How it evaluates: - `$<CXX_COMPILER_ID:GNU,Clang,AppleClang>` is `1` when the C++ compiler's id matches any entry, `0` otherwise. Before CMake 3.15 it accepted only a single id. - `$<1:-Wall;-Wextra>` becomes the list `-Wall;-Wextra`, which `target_compile_options` passes as two flags; `$<0:...>` becomes empty. - `$<CXX_COMPILER_ID>` without arguments returns the id itself, useful when debugging. What each warning flag does is a question for the compiler material; here the concern is getting the right flags to the right compiler. ## Trap 1: Apple clang's id is AppleClang CMake gives Apple clang its own id, `AppleClang`, because its version numbers differ from upstream Clang's. Policy `CMP0025` introduced this in CMake 3.0, and since CMake 4.0 the policy's old behaviour, reporting `Clang`, has been removed. A run with Apple clang 21 printed: | Expression | Value | |---|---| | `$<CXX_COMPILER_ID>` | `AppleClang` | | `$<CXX_COMPILER_ID:Clang>` | `0` | | `$<CXX_COMPILER_ID:Clang,AppleClang>` | `1` | A project testing only `Clang` therefore builds every macOS developer's code without the warnings, and nothing reports it. ## Trap 2: a space instead of a semicolon Inside the quoted expression, a space is part of the text, not a separator. This line: ```cmake target_compile_options(imgproc PRIVATE "$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wall -Wextra>") ``` puts the single argument `"-Wall -Wextra"` on the compile line. Apple clang answered `warning: unknown warning option '-Wall -Wextra'; did you mean '-Wextra'? [-Wunknown-warning-option]`, and neither warning set was enabled. Use `;` between list items inside generator expressions. ## Trap 3: unquoted spaces Written without quotes, `$<$<CXX_COMPILER_ID:GNU>:-Wall -Wextra>` is split by the CMake language into two arguments at the space. The manual's quoting section warns that such splitting can leave pieces that are no longer recognized as a generator expression. In a CMake 4.4.4 run, `target_compile_options` happened to rejoin the two pieces as a list and produce `-Wall -Wextra` correctly, but the same habit breaks in `add_custom_command` and elsewhere. Quote whole expressions and use `;`. ## Beyond the C++ compiler id - **Mixed-language targets.** `$<CXX_COMPILER_ID:...>` describes the C++ compiler even when a target also compiles C sources. `$<COMPILE_LANG_AND_ID:CXX,GNU,Clang,AppleClang>` tests the language of the source file being compiled and the compiler for it, so C files do not receive C++-only flags. - **Compiler versions.** `$<VERSION_LESS:$<CXX_COMPILER_VERSION>,13>` and related comparisons gate flags that only newer compilers understand. - **Frontend variant.** clang-cl reports the id `Clang` but takes MSVC-style options, so `$<CXX_COMPILER_FRONTEND_VARIANT:MSVC>` exists for that case. ## Keeping it readable Long nested expressions are hard to review. A common style names the condition first: 1. `set(gcc_like "$<CXX_COMPILER_ID:GNU,Clang,AppleClang>")` 2. `set(msvc "$<CXX_COMPILER_ID:MSVC>")` 3. `target_compile_options(imgproc PRIVATE "$<${gcc_like}:-Wall;-Wextra>" "$<${msvc}:/W4>")` The variables are expanded at configure time into the full expression, which is then evaluated at generate time, so this costs nothing and reads like a table. ## Proving the flags arrived Because every trap above fails silently, verify the result instead of trusting the expression: - **Read the real compile line.** A verbose build (`cmake --build build -v`) or `compile_commands.json` from `CMAKE_EXPORT_COMPILE_COMMANDS` shows exactly which options each source received. Look for two separate `-Wall` and `-Wextra` arguments, not one quoted string. - **Print the id CMake detected.** The configure output line `The CXX compiler identification is AppleClang ...` names the id to put in the list; `$<CXX_COMPILER_ID>` written out with `file(GENERATE ... TARGET imgproc)` shows it at generate time. - **Plant a warning.** A deliberately unused variable in a scratch source should produce a warning on every compiler the team supports; if one platform stays quiet, its id is missing from the list. Run these checks on each platform in CI once, then whenever someone edits the expression.
- Why would $<COMPILE_LANG_AND_ID:CXX,GNU,Clang,AppleClang> be preferred over $<CXX_COMPILER_ID:...> on some targets?When a target compiles both C and C++ sources. `$<CXX_COMPILER_ID>` describes the C++ compiler only, so a C++-only flag guarded by it would also be passed to C files. `$<COMPILE_LANG_AND_ID>` checks the language of each source being compiled together with that language's compiler id.
- Should warning flags be PUBLIC on a library target?Usually not. Warning flags are the library's own build policy, not a requirement for using it, and the CMake manual says usage requirements must be requirements rather than recommendations. Keep them `PRIVATE`, or put them on an interface target that each project target links privately.
- $<CXX_COMPILER_ID:Clang> also matches Apple clang
- A space separates two flags inside a quoted generator expression
- Generator expressions never need quotes
- A wrong compiler id stops the configure step with an error
Assumes CMake 4.x: AppleClang has been the only id for Apple clang since CMake 4.0 removed policy CMP0025's old behaviour; comma-separated id lists need CMake 3.15 or newer.