As a response to your problem you can also simply invoke the compiler like:
cc -c -DDEBUG=1
or
cc -c -DDEBUG=0
You must delete the "define DEBUG 1/0" in your files - or replace it with:
#ifndef DEBUG
#define DEBUG 0
#endif
Here is what I am using (GCC syntax):
create a file debug.h with the following content and include it in each c file:
#ifdef DEBUG
extern FILE *dbgf;
#define D_MIN 0x00010000 // Minimum level
#define D_MED 0x00020000 // Medium level
#define D_MAX 0x00040000 // Maximum level
#define D_FLUSH 0x00080000 // Usefull by a program crash
#define D_TRACE 0x00100000
#define D_1 0x00000001
...
#define D(msk, fmt, args...) if(msk & dbgmsk) { fprintf(dbgf, "%s:",__FUNCTION__); fprintf(dbgf, fmt, ## args ); if(msk & D_FLUSH) fflush(dbgf); }
#define P(msk, fmt, args...) if(msk & dbgmsk) { fprintf(dbgf, fmt, ## args ); if(msk & D_FLUSH) fflush(dbgf); }
#else
#define D(msk, fmt, args...)
#define P(msk, fmt, args...)
#endif
dbgmsk is variable, which can be global (whole program) or local/static and must be initialized a start. You can define several options for the whole program or for each module. This is better and more flexible than the version with the level variable.
Ex.
module1.c:
#include "debug.h"
static int dbgmsk; // using local dbgmsk
module1_setdbg(int msk) { dbgmsk = msk; D(D_TRACE,"dbgmsk1=%x\n", dbgmsk); }
foo1() { P(D_1, "foo1 function\n" );
....
}
foo2() {}
...
foo3.c
#include "debug.h"
extern int dbgmsk; // using global dbgmsk
Ex. main:
#include "debug.h"
FILE *dbgf;
int dbgmsk = 0; // this is the global dbgmsk
int main() {
dbgf = stderr; // or your logfile
dbgmsk = D_MIN;
module1_setdbg(D_MIN|D_MED|D_TRACE|D_1);
....
}
I'm also storing all dbgmsk variables in a config text file that is read at the program start.