Reputation: 2744
I have a cmake
/gcc
project in which I have enabled several warnings and the flag -Werror
.
I have noticed that some warnings are detected when I use the cmake
flag -DCMAKE_BUILD_TYPE=Release
, but they are not when I don't apply the above cmake
flag. For example, one of these warnings is:
error: ‘var_name’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
I have read here: Set CFLAGS and CXXFLAGS options using CMake that there are several CMAKE_C_FLAGS
variables for different build types, for instance, CMAKE_C_FLAGS_RELEASE
.
I have tried to apply those variables for release and debug builds, but this time none of the above detect the warnings I'm expecting.
What I'm missing?
Upvotes: 1
Views: 518
Reputation: 215617
CMake's default/"debug" build profile entirely disables optimization, which prevents the compiler passes that perform transformations and static analysis needed to determine that a variable is used uninitialized from ever happening. While to some extent this improves debugging experience with single-step of source lines, as you've found it hides warnings and also tends to hide consequences of undefined behavior in your code.
Traditionally "disable optimizations entirely for non-release builds" has not been a thing among unix-oriented developers. It's a carry-over from common practice in the MSVC world that's reflective of CMake's origins and user base.
Upvotes: 2