home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
1
/
1260
< prev
next >
Wrap
Internet Message Format
|
1990-12-28
|
2KB
From: escher@Apple.COM (Michael Crawford)
Newsgroups: comp.lang.c,comp.unix.wizards,alt.sources,comp.sources.d,misc.misc
Subject: Re: #define DEBUG... (using printf for debugging)
Message-ID: <8060@goofy.Apple.COM>
Date: 3 May 90 21:55:38 GMT
In article <1990May3.192347.12973@cs.umn.edu> thornley@cs.umn.edu (David H. Thornley) writes:
>In article <40628@cornell.UUCP> gordon@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@apple.com
Santa Cruz, CA 95060 Applelink: escher@apple.com@INTERNET#
oddball!mike@ucscc.ucsc.edu The opinions expressed here are solely my own.
Free Lithuania.