Warnings in build systems are often ignored because they don't break the build. But ignoring them can lead to hidden problems and future breakage - especially when CMake deprecates features or when you accidentally rely on unintended behavior. A good practice is to treat warnings as errors, forcing yourself (or your team) to fix them early rather than letting them pile up. This is common with compiler warnings (-Werror
option in GCC or Clang), and CMake has a similar mechanism. This tutorial explains how to treat CMake warnings as errors.
Consider the following CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.9)
project(myapp)
set(CMAKE_CXX_STANDARD 17)
add_executable(${PROJECT_NAME} main.cpp)
If you generate build files with a modern CMake, you may see this warning:
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 3.10 will be removed from a future version of
CMake.
Update the VERSION argument <min> value. Or, use the <min>...<max> syntax
to tell CMake that the project requires at least <min> but has been updated
to work with policies introduced by <max> or earlier.
Since this is only a warning, the build still succeeds - and it's easy to miss.
You can tell CMake to treat deprecation warnings as errors:
cmake -S . -B build -Werror=deprecated
Now the same project will fail at the configure step with a CMake Deprecation Error:
CMake Deprecation Error at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 3.10 will be removed from a future version of
CMake.
Update the VERSION argument <min> value. Or, use the <min>...<max> syntax
to tell CMake that the project requires at least <min> but has been updated
to work with policies introduced by <max> or earlier.
...
-- Configuring incomplete, errors occurred!
CMake also has developer warnings: these are hints about suspicious usage and questionable patterns. Normally, they're just advisory, but you can make them as error too:
cmake -S . -B build -Werror=dev
When to use these options:
-Werror=deprecated
- ensures the project doesn't rely on features that will disappear in future CMake releases.-Werror=dev
- keeps the CMake codebase clean by enforcing best practices and avoiding problematic constructs. By default, this will also turn on deprecated warnings as errors.
Leave a Comment
Cancel reply