#define DEBUG 1


I'm trying to have a debugging mode on so if

#define DEBUG 1

I want to printf some variable values and if

#define DEBUG 0

I want them off.

The problem is I have many implementation files and I want this DEBUG variable to be available for the whole project. Right now I need to edit the DEBUG variable in foo1.c, foo2.c, foo3.c which seems tedious and error-prone and there must be a better way. Any suggestions?

Best Solution

When compiling, you should be able to specify an option to your compiler. For example, you can call GCC with the -DDEBUG option.

In this case, you would be better using:

#ifdef DEBUG


#if defined(DEBUG)

if this is not the way you're doing it now. I'm surprised that you don't have a global header file for your project. Something along the lines of:

#undef DEBUG
#define DEBUG 1

in a file called "debug.h". In your C programs, you can include this by using #include "debug.h"