home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!wupost!sdd.hp.com!cs.utexas.edu!natinst.com!stepan
- From: stepan@natinst.com (Stepan Riha)
- Subject: Re: #pragma once whoes!!! Plea for help!
- Message-ID: <1992Sep1.002401.3359@natinst.com>
- Sender: news@natinst.com
- Nntp-Posting-Host: falcon.natinst.com
- Organization: National Instruments, Austin, TX
- References: <1992Aug31.135459.935@galaxy.gov.bc.ca> <1992Aug31.154247.937@galaxy.gov.bc.ca>
- Date: Tue, 1 Sep 1992 00:24:01 GMT
- Lines: 76
-
- In his message Carl says that he has "Multiply defined variable" link errors
- when he includes a file declaring variables.
-
- In article <1992Aug31.154247.937@galaxy.gov.bc.ca> cconstantine@galaxy.gov.bc.ca writes:
- >
- >BTW the conflict is with the following section in my AppGlobals.h unit:
- >
- >Boolean gQuitting;
- >Boolean gInBackground;
- >WindowPtr gMessageWindow;
- >RgnHandle gCursorRgn;
- >
- >It is these 4 globals that I am getting the Multiply defined error on. They
- >are in my AppGlobals.h file (as previously mentioned) and I've #included it in
- >3 files. NO OTHER CONSTANTS OR STRUCTS OR ANYTHING IS GETTING THE ERROR, JUST
- >THESE 4 VARIABLES!!!!!!!
- >
- > Carl B. Constantine
- > CCONSTANTINE@galaxy.gov.bc.ca
- > "Opinions are my own and are not necessarily those of my employer"
-
- I tried to mail a reply but it bounced :-(
-
- The mistake you're making is to *define* variables in a header file instead of
- *declaring* them. Since they are included in X files they are defined X
- time which is a no-no. Typedefs and #defines don't cause the problem because
- they just declare stuff for the compiler but don't allocate memory and the
- linker doesn't care about them.
- To fix this, put an "extern" in front of your globals in the .h file and
- declare them once in your .c file(s)
-
- Example:
-
- --- <AppGlobals.h> --------------------
- extern Boolean gQuitting; /* here you *declare* your variables */
- extern Boolean gInBackground;
- extern WindowPtr gMessageWindow;
- extern RgnHandle gCursorRgn;
- ---------------------------------------
-
- ---- <main.c> -------------------------
- #include "AppGlobals.h"
-
- Boolean gQuitting = FALSE, gInBackGround; /* dere you *define* variables */
-
- main(){
- ...
- ---------------------------------------
-
- ---- <windos.c> -----------------------
- #include "AppGlobals.h"
-
- WindowPtr gMessageWindow = NULL; /* here you define some more vars */
- RgnHandle gCursorRgn;
-
- InitMyWindow(){
- ...
- ---------------------------------------
-
- --- <support.c> -----------------------
- #include "AppGlobals.h"
-
- static Str255 theString;
-
- foo(){
- ...
- ---------------------------------------
-
- BTW, variables that are global to only one file should be declared static in
- that file.
- Any standard C book such as K&R tallks about these thing.
-
- - Stepan
- --
- Stepan Riha -- stepan@natinst.com
-
-