#define DEBUG... (using printf for debugging)

Michael Crawford escher at Apple.COM
Fri May 4 07:55:38 AEST 1990


In article <1990May3.192347.12973 at cs.umn.edu> thornley at cs.umn.edu (David H. Thornley) writes:
>In article <40628 at cornell.UUCP> gordon at cs.cornell.edu (Jeffrey  Adam Gordon) writes:
>>I want to have a DEBUG flag which controls whether diagnostic printfs
>>are executed or not.
>>
>>The obvious way to do this is:
>>
>>#ifdef DEBUG
>>	printf ("debugging information");
>>#endif DEBUG
>
>How about
>#ifdef DEBUG
>#define D(X) X
>#else
>#define D(X)
>#endif
>
>and 
>D(printf("debugging information\n");)

more elegant still:

#ifdef DEBUG
#define fDebug( x ) fprintf x
#define Debug( x ) printf x
#else
#define fDebug( x )
#define Debug( x )
#endif

fDebug(( stderr, "debugging info" ));
Debug(( "debugging info" ));

be sure to do the defining of DEBUG on the CC command line, with a nifty
setup in your makefile:

D=

CFLAGS = ${D}

foo: foo.o
	cc -o foo foo.o

foo.o: foo.c
	cc -c ${CFLAGS} foo.c

Then your command line might be:

alias md 'make D=-DDEBUG'
touch foo.c
md

and you will turn on debugging in foo.c.

Read Robert Ward's book "Debugging C".  It is a gold mine.  Mostly oriented
toward DOS, but much of what is in it is applicable anywhere.

Also, get a source level debugger.  You may have sdb or dbx on Unix systems,
SADE or ThinkC on Macintosh, or Codeview, and I think Turbo C, on the PC.  
The Free Software Foundation's GDB is available for Unix for free and is
much better than dbx or sdb, IMHO.

When you learn to use them effectively, it is a lot better than embedding
code in your source -- less recompiling.
-- 
Michael D. Crawford
Oddball Enterprises		Consulting for Apple Computer Inc.
606 Modesto Avenue		escher at apple.com
Santa Cruz, CA 95060		Applelink: escher at apple.com@INTERNET#
oddball!mike at ucscc.ucsc.edu	The opinions expressed here are solely my own.

		Free Lithuania.



More information about the Comp.unix.wizards mailing list