1

I usually debug my programs using print statements ( redirected to a log file). I place a #define _DEBUG at the top and place all the print statement between a #ifdef _DEBUG and #endif so that once I am convinced that program is running correctly I can change the #define and none of the print statements appear anymore.

The only disadvantage is that with all #ifdefs in the program, it is not at all readable because #ifdefs start from the beginning of the line.

Is there a way I can remove all the #ifdef,#endif and also all the lines between them ?

I can do this using recording if there are same number of print statements between define condition.

Graddy
  • 2,828
  • 5
  • 19
  • 25

2 Answers2

4

I'm sure you can remove them somehow, but why? You don't need to start #ifdefs at the start of the line, this is perfectly valid:

int getVal (void) {
    int x = 10
    #ifdef MY_DEBUG_FLAG
        printf ("Returning %d\n", x);
    #endif
    return x;
}

The other thing you should consider is not defining the variable directly in the source code with something like:

#define MY_DEBUG_FLAG

Most compilers have a switch which will do the same sort of thing before processing the source code:

gcc -DMY_DEBUG_FLAG ...

That way, you'll only need to change the global compiler flags in your makefile (or whatever build tool you use), almost certainly in one spot rather than every single source file.

And you can still selectively turn on debug for individual files if need be, by temporarily changing just the one compiler command. How easy this is depends on your build system but all that I've used make it rather painless.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • i know it is perfectly valid..but with the plugin and .vimrc that I am using it starts at the beginning of the line.. – Graddy Mar 01 '12 at 00:46
  • 1
    So change the plugin or vimrc. Your tools are supposed to adapt to you, not the other way around. I don't even allow auto indent, since I find it easier to bang on the tab key five times than do even one CTRL-D to unindent :-) – paxdiablo Mar 01 '12 at 00:47
1

A fairly common approach is to use a preprocessor macro to insert print statements that only get compiled during a debug build. This should help.

Community
  • 1
  • 1
Matt Kline
  • 10,149
  • 7
  • 50
  • 87