DEBUG macros in C++

后端 未结 9 1937
谎友^
谎友^ 2020-12-07 07:54

I just encountered a DEBUG macro in C that I really like

#ifdef DEBUG_BUILD
#  define DEBUG(x) fprintf(stderr, x)
#else
#  define DEBUG(x) do {} while (0)
#e         


        
9条回答
  •  半阙折子戏
    2020-12-07 08:30

    … and as addendum to all responses:

    Personally I never use macros like DEBUG to distinct debug from release code, instead I use NDEBUG which is must be defined for release builds to eliminate assert() calls (yes, I use assert() extensively). And if latter is not defined, then it is a debug build. Easy! So, actually there is no reason to introduce one more debug macro! (and handle possible cases when DEBUG and NDEBUG both are not defined).

提交回复
热议问题