How to obtain debug/release conditional compiling in C++ program

爷,独闯天下 提交于 2019-12-06 04:24:45

问题


In a large C++/Qt/QMake/qtcreator project I would like to perform some tests, but only when I am compiling with the debug flag.

Is there a way to tell g++ that some small parts of the code have to be compiled only in debug mode ?


回答1:


The standard way to do this is to depend on the macro NDEBUG, which is used by the macro assert() defined in <cassert>:

#ifdef NDEBUG
  // release mode code
#else
  // debug mode code
#endif

The opposite of #ifdef is #ifndef, and of course #else branches are optional.

If this macro doesn't work (for whatever reason), you

  1. can try the macro QT_NO_DEBUG, which Qt uses for a similar purpose with Q_ASSERT(); and

  2. should fix it so that NDEBUG is (un)defined correctly; it's required for <cassert> to work properly, and code you use may depend on it.



来源:https://stackoverflow.com/questions/22537251/how-to-obtain-debug-release-conditional-compiling-in-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!