Does CMAKE_BUILD_TYPE=Release imply -DNDEBUG?

patryk.beza picture patryk.beza · Dec 16, 2015 · Viewed 11.4k times · Source

Does CMAKE_BUILD_TYPE=Release implicitly imply -DNDEBUG?

If not: isn't it reasonable to expect that this implication takes place?

I want to know if following CMake code is redundant in my CMakeLists.txt:

if (NOT CMAKE_BUILD_TYPE MATCHES Debug)
    add_definitions(-DNDEBUG)
endif()

Answer

usr1234567 picture usr1234567 · Dec 16, 2015

Yes, it is set by CMake. Grepping through the CMake code reveals, that for a host of compilers it is set. Probably they set it only for these compilers, which accepts this flag. Here one of the lines concerning GCC:

Modules/Compiler/GNU.cmake:  set(CMAKE_${lang}_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")

But be aware that many projects overwrite release/debug flags without preserving the initial setting and also overwriting user's definitions.