Q: How to compile a program with and without "DEBUG" statements - TopicsExpress



          

Q: How to compile a program with and without "DEBUG" statements enabled ? Ans: For internal development purpose, the DEBUGs are enabled, and during release, the DEBUGs are disabled. It can be done through compile time debugging technique through VARIADIC MACROS. I have given a simple program( copy the contents of one file to another) to illustrate the use of VARIADIC MACROS for debugging purpose. Compile it as given below. Check the difference in output in both cases. WARNING: If you want to compile this program, please type the program by yourself on the editor. If you would copy and paste it on your editor, there would be lot of stray elements which your editor will not understand and would result in a compilation error !! Stray elements are those special characters (like / , * , " " ) that are differently interpreted by different editors. Beware of them!! RELEASE MODE: ========== $gcc debug.c $./a.out Note: Debug statements disabled here. DEBUG MODE: ========= $gcc -DDEBUG debug.c $./a.out Note: Debug statements enabled here. SOURCE CODE: ========= /* debug.c */ #include #include #ifdef DEBUG #define DEBUG 1 #else #define DEBUG 0 #endif /* VARIADIC MACRO */ #define DBGPRINT(x, ...) if( DEBUG ) { fprintf( stderr, "%s:%d:%s" x " ", __FILE__, __LINE__, __func__, ##__VA_ARGS__ ); } int main(int argc, char *argv[]) { FILE *fp; int c,i=0; char s[100]; DBGPRINT("I am here %d", 1 ); if(argc !=3) { fprintf(stderr,"USAGE: "); exit(0); } if((fp = fopen(argv[1],"r")) == NULL) DBGPRINT("I am here %d", 2 ); if((fp = fopen(argv[1],"r")) == NULL) { fprintf(stderr,"Cannot open file for reading "); exit(1); } DBGPRINT("I am here %d",3 ); while(( c = fgetc(fp)) != EOF) { DBGPRINT("I am here %d and my name is %s", 4, "subhash" ); s[i] = c; i++; } s[i] = EOF; fclose(fp); DBGPRINT("I am here %d", 5 ); if((fp = fopen(argv[2],"w")) == NULL ) { fprintf(stderr,"Error in opening of file "); exit(2); } i =0; DBGPRINT("I am here %d", 6 ); while( s[i] != EOF) { fputc(s[i],fp); i++; } DBGPRINT("I am here %d", 7 ); fclose(fp); return 0; }
Posted on: Wed, 10 Jul 2013 06:45:38 +0000

Trending Topics



Recently Viewed Topics




© 2015