home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / mac / programm / 14804 < prev    next >
Encoding:
Text File  |  1992-08-31  |  2.7 KB  |  89 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!wupost!sdd.hp.com!cs.utexas.edu!natinst.com!stepan
  3. From: stepan@natinst.com (Stepan Riha)
  4. Subject: Re: #pragma once whoes!!! Plea for help!
  5. Message-ID: <1992Sep1.002401.3359@natinst.com>
  6. Sender: news@natinst.com
  7. Nntp-Posting-Host: falcon.natinst.com
  8. Organization: National Instruments, Austin, TX
  9. References: <1992Aug31.135459.935@galaxy.gov.bc.ca> <1992Aug31.154247.937@galaxy.gov.bc.ca>
  10. Date: Tue, 1 Sep 1992 00:24:01 GMT
  11. Lines: 76
  12.  
  13. In his message Carl says that he has "Multiply defined variable" link errors
  14. when he includes a file declaring variables.
  15.  
  16. In article <1992Aug31.154247.937@galaxy.gov.bc.ca> cconstantine@galaxy.gov.bc.ca writes:
  17. >
  18. >BTW the conflict is with the following section in my AppGlobals.h unit:
  19. >
  20. >Boolean        gQuitting;
  21. >Boolean        gInBackground;
  22. >WindowPtr    gMessageWindow;
  23. >RgnHandle    gCursorRgn;
  24. >
  25. >It is these 4 globals that I am getting the Multiply defined error on.  They
  26. >are in my AppGlobals.h file (as previously mentioned) and I've #included it in
  27. >3 files.  NO OTHER CONSTANTS OR STRUCTS OR ANYTHING IS GETTING THE ERROR, JUST
  28. >THESE 4 VARIABLES!!!!!!!
  29. >
  30. > Carl B. Constantine
  31. > CCONSTANTINE@galaxy.gov.bc.ca
  32. >     "Opinions are my own and are not necessarily those of my employer"
  33.  
  34. I tried to mail a reply but it bounced :-(
  35.  
  36. The mistake you're making is to *define* variables in a header file instead of
  37. *declaring* them.  Since they are included in X files they are defined X
  38. time which is a no-no.  Typedefs and #defines don't cause the problem because
  39. they just declare stuff for the compiler but don't allocate memory and the
  40. linker doesn't care about them.
  41. To fix this, put an "extern" in front of your globals in the .h file and
  42. declare them once in your .c file(s)
  43.  
  44. Example:
  45.  
  46. --- <AppGlobals.h> --------------------
  47. extern Boolean        gQuitting;       /* here you *declare* your variables */
  48. extern Boolean        gInBackground;
  49. extern WindowPtr    gMessageWindow;
  50. extern RgnHandle    gCursorRgn;
  51. ---------------------------------------
  52.  
  53. ---- <main.c> -------------------------
  54. #include "AppGlobals.h"
  55.  
  56. Boolean gQuitting = FALSE, gInBackGround;    /* dere you *define* variables */
  57.  
  58. main(){
  59. ...
  60. ---------------------------------------
  61.  
  62. ---- <windos.c> -----------------------
  63. #include "AppGlobals.h"
  64.  
  65. WindowPtr gMessageWindow = NULL;    /* here you define some more vars */
  66. RgnHandle gCursorRgn;
  67.  
  68. InitMyWindow(){
  69. ...
  70. ---------------------------------------
  71.  
  72. --- <support.c> -----------------------
  73. #include "AppGlobals.h"
  74.  
  75. static Str255 theString;
  76.  
  77. foo(){
  78. ...
  79. ---------------------------------------
  80.  
  81. BTW, variables that are global to only one file should be declared static in
  82. that file.
  83. Any standard C book such as K&R tallks about these thing.
  84.  
  85.     - Stepan
  86. -- 
  87.    Stepan Riha -- stepan@natinst.com
  88.  
  89.