Remove CMake internal definition like -DNDEBUG
For C or C++ projects with incorrect #define logic or due to compiler bugs, it may be necessary to avoid CMake internally set definitions like -DNDEBUG.
CMake internally sets -DNDEBUG when the CMAKE_BUILD_TYPE is set to Release, RelWithDebInfo, or MinSizeRel.
This can be done in scope like:
string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")remove_definitions(-DNDEBUG) does not work here because -DNDEBUG is set internal to CMake.
The same may be accomplished per target by manipulating target COMPILE_DEFINITIONS:
get_target_property(_cf my_target COMPILE_DEFINITIONS)
string(REPLACE "-DNDEBUG" "" _cf "${_cf}")
set_target_properties(my_target PROPERTIES COMPILE_DEFINITIONS "${_cf}")