Sometimes, it is handy to add an option that is active only in
Debug builds. When doing this, keep in mind that tests like
IF(WITH_DEBUG)
or
IF(CMAKE_BUILD_TYPE MATCHES "Debug")
do not
work as expected:
First, although
WITH_DEBUG
is an alias forCMAKE_BUILD_TYPE=Debug
, the converse is not true.Second, checking for
CMAKE_BUILD_TYPE
will not work everywhere. More precisely, it will not work with multi-configuration CMake generators (that is, neither on Windows with Visual Studio nor on Mac OS X with Xcode).
So, when adding debug-only options, consider extending
CMAKE_C_FLAGS_DEBUG
and
CMAKE_CXX_FLAGS_DEBUG
. For example:
# Works always
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DUNIV_DEBUG")
Do not do it like this:
IF(WITH_DEBUG)
# Does NOT work with CMAKE_BUILD_TYPE=Debug, Visual Studio or Xcode
ADD_DEFINITIONS(-DUNIV_DEBUG)
ENDIF()