This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: Debug define name
- From: "John (Eljay) Love-Jensen" <eljay at adobe dot com>
- To: Paulo Flabiano Smorigo <pfsmorigo at gmail dot com>, GCC Help List <gcc-help at gnu dot org>
- Date: Wed, 22 Jul 2009 06:42:43 -0700
- Subject: RE: Debug define name
- References: <53934def0907220543m6932086diceeac0197a4a553a@mail.gmail.com>
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