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 >
Internet Message Format  |  1990-12-28  |  2KB

  1. From: escher@Apple.COM (Michael Crawford)
  2. Newsgroups: comp.lang.c,comp.unix.wizards,alt.sources,comp.sources.d,misc.misc
  3. Subject: Re: #define DEBUG... (using printf for debugging)
  4. Message-ID: <8060@goofy.Apple.COM>
  5. Date: 3 May 90 21:55:38 GMT
  6.  
  7. In article <1990May3.192347.12973@cs.umn.edu> thornley@cs.umn.edu (David H. Thornley) writes:
  8. >In article <40628@cornell.UUCP> gordon@cs.cornell.edu (Jeffrey  Adam Gordon) writes:
  9. >>I want to have a DEBUG flag which controls whether diagnostic printfs
  10. >>are executed or not.
  11. >>
  12. >>The obvious way to do this is:
  13. >>
  14. >>#ifdef DEBUG
  15. >>    printf ("debugging information");
  16. >>#endif DEBUG
  17. >
  18. >How about
  19. >#ifdef DEBUG
  20. >#define D(X) X
  21. >#else
  22. >#define D(X)
  23. >#endif
  24. >
  25. >and 
  26. >D(printf("debugging information\n");)
  27.  
  28. more elegant still:
  29.  
  30. #ifdef DEBUG
  31. #define fDebug( x ) fprintf x
  32. #define Debug( x ) printf x
  33. #else
  34. #define fDebug( x )
  35. #define Debug( x )
  36. #endif
  37.  
  38. fDebug(( stderr, "debugging info" ));
  39. Debug(( "debugging info" ));
  40.  
  41. be sure to do the defining of DEBUG on the CC command line, with a nifty
  42. setup in your makefile:
  43.  
  44. D=
  45.  
  46. CFLAGS = ${D}
  47.  
  48. foo: foo.o
  49.     cc -o foo foo.o
  50.  
  51. foo.o: foo.c
  52.     cc -c ${CFLAGS} foo.c
  53.  
  54. Then your command line might be:
  55.  
  56. alias md 'make D=-DDEBUG'
  57. touch foo.c
  58. md
  59.  
  60. and you will turn on debugging in foo.c.
  61.  
  62. Read Robert Ward's book "Debugging C".  It is a gold mine.  Mostly oriented
  63. toward DOS, but much of what is in it is applicable anywhere.
  64.  
  65. Also, get a source level debugger.  You may have sdb or dbx on Unix systems,
  66. SADE or ThinkC on Macintosh, or Codeview, and I think Turbo C, on the PC.  
  67. The Free Software Foundation's GDB is available for Unix for free and is
  68. much better than dbx or sdb, IMHO.
  69.  
  70. When you learn to use them effectively, it is a lot better than embedding
  71. code in your source -- less recompiling.
  72. -- 
  73. Michael D. Crawford
  74. Oddball Enterprises        Consulting for Apple Computer Inc.
  75. 606 Modesto Avenue        escher@apple.com
  76. Santa Cruz, CA 95060        Applelink: escher@apple.com@INTERNET#
  77. oddball!mike@ucscc.ucsc.edu    The opinions expressed here are solely my own.
  78.  
  79.         Free Lithuania.
  80.