Just learn cmake, a lot of things still not very understand, but today just realized through the CMake control debug switch, the excitement of recording a bit.
Background introduction:
Recently involved in a large C + + project, a lot of code is very mature, I have to add some accessibility, but the source code is not reserved for debug interface. In order to not affect the structure of existing code, I want to add the debug control in a way that is defined by the macro. It was thought that # define debug can only be defined in. h or. cc files, and then the essence of #ifdef debug is realized through CMake.
Specific steps:
First, I wrote a number of debug outputs in the SRC code:
#IFDEF DEBUG some print command;#ENDIF
Then, add the definition of debug in Cmakefile:
IF (CMAKE_BUILD_TYPE STREQUAL Debug) 用IF控制,可以实现Release版本不输出debug信息 ADD_DEFINITIONS(-DDEBUG) 不知道为什么DEBUG必须写成-DDEBUG,谁来解释一下吧ENDIF()
Finally, set the build type to debug in the CMake command:
$ cmake ../src -DCMAKE_BUILD_TYPE=Debug$ make
Debug messages such as some print command are executed when the executable is run again. If you do not want to see the debug information, simply change the debug to release:
$ cmake ../src -DCMAKE_BUILD_TYPE=Release
C + + implements the debug switch via CMake