In C (and C++) file scope variables are 'extern' (global) by default.
Declaring the variable as 'static' limits the scope to the file.

In other words, if you don't the header files in each file that uses the
variables you get a 'not defined' error because the symbol is not
defined.  If you include a simple:

int Foo;

type statement in each .c (or .cpp) file you are trying to create a new
global (extern) variable in each file.  So, you get a warning from the
compiler.

If you include:

static int Foo;

type statements in each .c (or .cpp) file you are creating a unique set
of variables for each file (the scope is file scope, not global).  I
suspect that "work" is subjective.  Depending upon what you are doing
with the variables, the fact that each file actually has its own copy
will manifest different symptoms.

If you really want to use global variables (something I do not generally
recommend), what you want is *1* file to include statements like:

int Foo;

And all the other files to include statements like:

extern int Foo;

Way back, people used to maintain two files (usually globals.h and
externs.h).  Now, it is more common to use the pre-processor.  Basically
you use a define like PUBLIC:

PUBLIC int Foo;

You would then use the preprocessor and external defines to equate
PUBLIC to either "extern" or "".

Good Luck,
-jjf

-----Original Message-----
From: Sam Charette [mailto:[EMAIL PROTECTED]]
Sent: Monday, May 01, 2000 11:25 AM
To: Palm Developer Forum
Subject: A stumper...


I have a question for anyone who is willing to help me out in my time of
utter confusion.

I am doing a proof of concept (ergo little time for design, and therefor
not very well structured) for a project and am having problems with
certain global variables.

In the main application header file I create global variables for each
of the databases to be used by the application.  I use the:

#ifndef <identifier>
#define <identifier>
#endif

group in order to include the contents of my header files only once
regardless as to how often it is included by other files.  I must,
however, for some reason declare my globals as static since if I don't I
get multiple definition errors.

Now, I have 3 different files accessing these globals.  Two of them work
fine, one does not.  When I get to that file the database variable being
used seems to have been reset and so I get a rather nasty error when I
try to access it with the DmNumRecords function.

I really don't understand why the linker thinks there are multiple
declarations of these variables if not set static, and I do not
understand why two of the files would work and one would not.  They all
have the proper headers included...

Any help would be greatly appreciated!

Sam Charette


-- 
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palm.com/devzone/mailinglists.html

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palm.com/devzone/mailinglists.html

Reply via email to