C + + implements the debug switch via CMake

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.