184

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?

7 Answers 7

148

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.

4
  • 25
    +1. NDEBUG in particular is allowed to be #undef'd and #define'd within a single TU (and reincluding <assert.h> changes the assert macro accordingly). Because this is different than expected/desired, it's common to use another macro to control a "compile-wide" debug flag as this answer says.
    – Roger Pate
    Commented Feb 18, 2010 at 17:23
  • 3
    Apparently, it's the compiler itself that defines these macros and not Visual Studio (the IDE). Thanks a lot for this answer!
    – Niklas R
    Commented Jul 24, 2015 at 11:26
  • NDEBUG isn't defined when using applications templates from Windows Driver Kit 8.1 too. In this case, you may need to rely on _DEBUG.
    – navossoc
    Commented Apr 7, 2016 at 15:36
  • learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros states that the /LDd compiler option will also cause VS to define _DEBUG. @Christoph, maybe this is worth editing into your answer?
    – AJM
    Commented Sep 8, 2022 at 13:54
87

Is NDEBUG standard?

Yes it is a standard macro with the semantic "Not Debug" for C89, C99, C++98, C++2003, C++2011, C++2014 standards. There are no _DEBUG macros in the standards.

C++2003 standard send the reader at "page 326" at "17.4.2.1 Headers" to standard C.

That NDEBUG is similar as This is the same as the Standard C library.

In C89 (C programmers called this standard as standard C) in "4.2 DIAGNOSTICS" section it was said

https://port70.net/~nsz/c/c89/c89-draft.html

If NDEBUG is defined as a macro name at the point in the source file where <assert.h> is included, the assert macro is defined simply as

     #define assert(ignore) ((void)0)

If look at the meaning of _DEBUG macros in Visual Studio https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros then it will be seen, that this macro is automatically defined by your сhoice of language runtime library version.

0
59

I rely on NDEBUG, because it's the only one whose behavior is standardized across compilers and implementations (see documentation for the standard assert macro). The negative logic is a small readability speedbump, but it's a common idiom you can quickly adapt to.

To rely on something like _DEBUG would be to rely on an implementation detail of a particular compiler and library implementation. Other compilers may or may not choose the same convention.

The third option is to define your own macro for your project, which is quite reasonable. Having your own macro gives you portability across implementations and it allows you to enable or disable your debugging code independently of the assertions. Though, in general, I advise against having different classes of debugging information that are enabled at compile time, as it causes an increase in the number of configurations you have to build (and test) for arguably small benefit.

With any of these options, if you use third party code as part of your project, you'll have to be aware of which convention it uses.

2
  • 1
    #if !defined(NDEBUG) <-- @HostileFork isn't that what you meant? #if not #ifdef?
    – Bob Stein
    Commented Apr 5, 2019 at 12:52
  • 1
    Original comment corrected via @BobStein: "Something I've taken to doing to (slightly) mitigate the readability "speedbump" is that when talking about a no-debug condition use #ifdef NDEBUG...but then call special attention to the negative logic with #if !defined(NDEBUG). Otherwise it's a little hard to catch the n in #ifndef NDEBUG" Commented Apr 5, 2019 at 21:50
27

The macro NDEBUG controls whether assert() statements are active or not.

In my view, that is separate from any other debugging - so I use something other than NDEBUG to control debugging information in the program. What I use varies, depending on the framework I'm working with; different systems have different enabling macros, and I use whatever is appropriate.

If there is no framework, I'd use a name without a leading underscore; those tend to be reserved to 'the implementation' and I try to avoid problems with name collisions - doubly so when the name is a macro.

5
  • 2
    Can you explain why you consider asserts distinct from other types of debugging? In what scenarios would you want one and not the other? Commented Aug 13, 2013 at 15:33
  • 5
    I keep asserts active even when I don't compile other debugging or tracing facilities into the code. Assertions identify impossible situations. General tracing and debugging is IMO wholly separate from that. Commented Aug 13, 2013 at 15:41
  • 3
    @AdrianMcCarthy: if you have ALL debugging enabled, it can cause programs to run INCREDIBLY slowly. So it's common to categorize types of debugging, and allow people to enable/disable them separately. MSVC has two or three they use to enable/disable various debugging for the standard library, like std::vector. Commented Nov 8, 2013 at 19:23
  • @MooingDuck: Agreed, but distinguish between 'debugging available and enabled' and 'debugging available but disabled' and 'debugging not available'. The available/not available decision is a compile time issue (in C or C++ code), and the enabled/disabled is then a runtime decision when the debugging is available. Only the crudest debugging systems work with 'available means enabled' mode. (That said, crude can be effective, and there are an awful lot of crude - but effective enough - debugging systems out there in the wild!) Commented Nov 8, 2013 at 19:48
  • 2
    @MooingDuck: Yes, some debugging code may be vary slow. In that case, you might want a way to control whether the slow code runs independently from the trivial checks. But that's not the same as putting assertions in one category and #if-controlled code in the other. assert(huge_object.IsValid()); might be slow while assert(ptr != nullptr); probably isn't. I agree with Jonathan that logging and tracing should probably be distinct from assertions, at least in larger projects, but I don't think of logging or tracing as debug code, which is why I asked for clarification. Commented Nov 8, 2013 at 23:44
8

Be consistent and it doesn't matter which one. Also if for some reason you must interop with another program or tool using a certain DEBUG identifier it's easy to do

#ifdef THEIRDEBUG
#define MYDEBUG
#endif //and vice-versa
5

Unfortunately DEBUG is overloaded heavily. For instance, it's recommended to always generate and save a pdb file for RELEASE builds. Which means one of the -Zx flags, and -DEBUG linker option. While _DEBUG relates to special debug versions of runtime library such as calls to malloc and free. Then NDEBUG will disable assertions.

2

Despite the name, NDEBUG has nothing to do if you are creating a debug build or not, it controls whether assertions (assert()) are active or not. I would not base anything else on it, as you may want to have debug builds without assertions or release builds with assertions from time to time and then you must set NDEBUG accordingly but that doesn't mean you also want all other code to be debug or release code.

From the perspective of compilers, there is not such thing as a debug build. You tell the compiler to build code with a specific set of settings and if you want to use different settings for different kinds of builds, then this is something you actually made up yourself and the compiler knows nothing about that. You may actually have 50 different build styles and not just release and debug (profile, test, deploy, etc.), so it's up to you how these styles are identified in your own code. If you need pre-processor tags for these, you define how those are named and the same name space rules applies as for everything else you'd define in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.