Advertisement

static & extern

Started by February 22, 2003 05:17 AM
4 comments, last by UnsilentStorm 21 years, 8 months ago
Sorry this isn''t much related to games but to programming in general. I have a project with several files, in one of these i declare a static variable, and i want to use it also other files. In those other files, have I to use the extern costruct or can I live with happyness? Infact the compiler is happy only if i redeclare the same variable in the other files! Ex: in main file, I have: static int foo; Only if i rewrite in the other files: static int foo; it''s happy.. My question is, those are the same variable? Thanks in advance for possible answers on this bad question! Fire burn wisdom in me, Wisdom set mind and spirit free, Moonlight shows me the mysteries of life, Winternight gives me clearsight and storms to fight.
Fire burn wisdom in me,Wisdom set mind and spirit free,Moonlight shows me the mysteries of life,Winternight gives me clearsight and storms to fight.
static makes a variable valid just file wise, to see it in the other files you must declare it extern in the files you want to use it. ie:

in main file:
static int i;
i=5;

in others:
extern static int i; //i == 5



To be considered a genius you just have to say what everybody knows in a way very few understand
To be considered a genius you just have to say what everybody knows in a way very few understand
Advertisement
static as a storage class defines internal (to the translation unit) linkage. If you have a static variable it cannot be accessed from outside that translation unit. Defining static variables in two different translation units with the same name will create separate variables. i.e. static int i in temp1.cpp will be independent of static int i in temp2.cpp.

If you wish to use a global variable, say int i, in multiple source files, the accepted practice is place "extern int i;" in a header file and include that header in all source files that require access to the variable. Then, in a single source file place the line "int i;" (note lack of storage qualifiers; if you leave the static keyword, you''ll get linkage errors). Then references to i will refer to the same variable.

extern and static are incompatible. A standards compliant compiler will barf on extern static int i (probably with an error like "multiple storage classes in definition of i").
Ok, thanks for the hints!

Fire burn wisdom in me,
Wisdom set mind and spirit free,
Moonlight shows me the mysteries of life,
Winternight gives me clearsight and storms to fight.
Fire burn wisdom in me,Wisdom set mind and spirit free,Moonlight shows me the mysteries of life,Winternight gives me clearsight and storms to fight.
Ok, i solved my problem. I did as follows.

I have 4 files, control.h, control.cpp, graphplan.cpp and graphplan.h. In graphplan.cpp i have the main function (not the ''main'' literally, but the starting function of my system, named ''planner'', ''cause i am building a library), and here i need to reinitialize the static variables to an initial value, so at each call of the function ''planner'' the static variables have always the same value.

The declaration of the static variable (static int i, for example), was in control.cpp. I moved it in graphplan.h that is included by control.h that in turn is included by control.cpp, so I can use the static variable in control.cpp.

Moreover, graphplan.h is included by graphplan.cpp, so in the function ''planner'' i can set, for example, int i = 0 at the beginning of the function, thus setting i to 0 every time i call ''planner''.

Thanks for ur support!

Fire burn wisdom in me,
Wisdom set mind and spirit free,
Moonlight shows me the mysteries of life,
Winternight gives me clearsight and storms to fight.
Fire burn wisdom in me,Wisdom set mind and spirit free,Moonlight shows me the mysteries of life,Winternight gives me clearsight and storms to fight.
That being said, usage of static at file scope is deprecated. Proper C++ would be :

namespace{  int variable_with_internal_visibility;  int function_with_internal_visibility();} 


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement