This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Debug define name


Hi Paulo,

Since the early days of K&R C and Unix, the define NDEBUG has been used, and is still used by the C <assert.h> header file, and by the C++ <cassert> header file.

To get the debug behavior, you need to compile like this:

gcc Foo.c

To get the release (non-debug) behavior, you need to compile like this:

gcc -DNDEBUG Foo.c

To isolate debug-only code blocks, you do this:

#ifndef NDEBUG
  DebugTrace("yada yada");
#endif

To isolate release-only code blocks, you do this:

#ifdef NDEBUG
  ReleaseOnly("yada yada");
#endif

To create a (say) TRACE macro that is only in your code for debug-only, you could do this:

#include <iostream>
#ifdef NDEBUG
#define TRACE(x) ((void)0)
#else
#define TRACE(x) std::cerr << x << std::endl
#endif

The use of _DEBUG is something I've only run across in Microsoft's headers.  (It may be used by others as a convention, but I haven't bumped into that yet.)

You are at liberty to use your own preprocessor symbol to indicate debug-vs-nondebug compilations.  DEBUG, _DEBUG, __DEBUG__, __DEBUG or whatever.  Be warned, though, that _DEBUG, __DEBUG, __DEBUG__ are all reserved symbols.  So you are better off using DEBUG or PFS_DEBUG (for examples).

I find NDEBUG a little confusing because of the double-negative #ifndef NDEBUG, but that's just me.  I'm a bear of small brain.

HTH,
--Eljay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]