_DEBUG vs NDEBUG

deft_code picture deft_code · Feb 18, 2010 · Viewed 150k times · Source

Which preprocessor define should be used to specify debug sections of code?

Use #ifdef _DEBUG or #ifndef NDEBUG or is there a better way to do it, e.g. #define MY_DEBUG?

I think _DEBUG is Visual Studio specific, is NDEBUG standard?

Answer

Christoph picture Christoph · Feb 18, 2010

Visual Studio defines _DEBUG when you specify the /MTd or /MDd option, NDEBUG disables standard-C assertions. Use them when appropriate, ie _DEBUG if you want your debugging code to be consistent with the MS CRT debugging techniques and NDEBUG if you want to be consistent with assert().

If you define your own debugging macros (and you don't hack the compiler or C runtime), avoid starting names with an underscore, as these are reserved.