The usual way suggested by the assert (3) man page and habits (from <assert.h> in C or <cassert> in C ++) is to define NDEBUG on the command line (for example, to compile with gcc -Wall -DNDEBUG ) to compile without debugging. In the Makefile you could CPPFLAGS += -DNDEBUG in release mode (and compile with g++ -Wall -g in debug mode).
My own habit may have something like
#ifndef NDEBUG #define dbgprintf(Fmt,...) do{fprintf(stderr,"%s:%d:" Fmt "\n", \ __FILE__, __LINE__, \ ##__VA_ARGS__);}while(0) #else #define dbgprintf(Fmt,...) do{}while(0) #endif
in the general header file and use dbgprintf("i=%d", i) elsewhere in the code. Note that I use the constant string binding in the Fmt macro Fmt , adding a newline constant to it, and that my debug output contains the source file name and line number (you can also use __func__ if you want). In pure C ++ code, I could
#ifndef NDEBUG #define DBGOUT(Out) do{std::out << __FILE__ << ":" << __LINE__ \ << " " << Out << std::endl;}while(0) #else #define DBGOUT(Out) do{}while(0) #endif
and use DBGOUT("i=" << i) in order to use special operator << definitions for my types.
source share