RE: Debug define name

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux