Shared global variables 
Author Message
 Shared global variables


Quote:
> For WHAT TYPES is this valid: arrays? int? pointers? instances of class?
> pointers to functions?

All data types--this is the way you get data shared across translation
units.

Quote:
> If it is valid for all types of variables, it means that I need two
> parallel .h files for global variables:
>    * one with all glabal variables prefixed with extern
>    * one with the same but unprefixed data

No, you actually don't--there are ways around this; for example, you could
use the proprocessor to get around it, and I believe that it's legal to
simply declare the variable as extern in your .h, then, in one file, you can
just declare it again, WITHOUT the extern keyword, AFTER you include your .h
file.


Fri, 04 Jan 2002 03:00:00 GMT  
 Shared global variables
Quote:

> If it is valid for all types of variables, it means that I need two
> parallel .h files for global variables:

>    * one with all glabal variables prefixed with extern
>    * one with the same but unprefixed data

The actual definition doesn't need to be in a header file. You can
simply put it into your .cpp file and be done with it. Thus, only one
set of header files, with 'extern' declarations. I also saw somewhere
another subterfuge designed to bypass this problem. Make a macro, in
some unique header file, something like this:

#ifdef MAIN
#define EXTERNAL_DEF // nothing
#else
#define EXTERNAL_DEF extern
#endif

then, in your extern-ridden headers declare your data with this macro:
EXTERNAL_DEF int globalInt;     // etc.

finally, in one (and only one) .cpp file, probably where your main() is
you
#define MAIN 1  // before any #include statements. This way, your
"extern" prefixes will be pre-processed away only in that file, which is
what you wanted in the first place. The (only) advantage of using this
system would be that all your global data are defined in one place, and
so if you need to change them you wont' have to fix to places up. I
would bother with this only if the headers/global data set are re-used.

--
len
if you must email, reply to:



Fri, 04 Jan 2002 03:00:00 GMT  
 Shared global variables
Thanks for your help! I really need to figure that out.
Surprises me that this subject is docmented NOWHERE!

PixelCat a crit :

Quote:

> All data types--this is the way you get data shared across translation
> units.

> >    * one with all glabal variables prefixed with extern
> >    * one with the same but unprefixed data

> No, you actually don't--there are ways around this; for example, you could
> use the proprocessor to get around it, and I believe that it's legal to
> simply declare the variable as extern in your .h, then, in one file, you can
> just declare it again, WITHOUT the extern keyword, AFTER you include your .h
> file.



Sat, 05 Jan 2002 03:00:00 GMT  
 Shared global variables
Thanks for your help! I really need to figure that out.
Surprises me that this subject is documented NOWHERE!

"U. Art" a crit :

Quote:

> The actual definition doesn't need to be in a header file. You can
> simply put it into your .cpp file and be done with it. Thus, only one
> set of header files, with 'extern' declarations. I also saw somewhere
> another subterfuge designed to bypass this problem. Make a macro, in
> some unique header file, something like this:

> #ifdef MAIN
> #define EXTERNAL_DEF // nothing
> #else
> #define EXTERNAL_DEF extern
> #endif

> then, in your extern-ridden headers declare your data with this macro:
> EXTERNAL_DEF int globalInt;     // etc.

> finally, in one (and only one) .cpp file, probably where your main() is
> you
> #define MAIN 1  // before any #include statements. This way, your
> "extern" prefixes will be pre-processed away only in that file, which is
> what you wanted in the first place. The (only) advantage of using this
> system would be that all your global data are defined in one place, and
> so if you need to change them you wont' have to fix to places up. I
> would bother with this only if the headers/global data set are re-used.

> --
> len
> if you must email, reply to:




Sat, 05 Jan 2002 03:00:00 GMT  
 Shared global variables
Quote:

> Thanks for your help! I really need to figure that out.
> Surprises me that this subject is documented NOWHERE!

De rien. Actually, it's documented all right--in any C language manual,
the section dealing with "extern" declarations <g>. Unless my memory
fails me, the trick I've described I stole from either Labrosse's book
on uCos or Burgess's "32-bit OS" book. It may be wrong though, I really
don't remember for sure.


Sat, 05 Jan 2002 03:00:00 GMT  
 Shared global variables

Quote:
> Thanks for your help! I really need to figure that out.
> Surprises me that this subject is docmented NOWHERE!

Actually, it is, but you'd have to search for it, probably, rather than
scanning the help directly.

In VC++5, Developer Products\Visual C++\C/C++ Language and C++ Libraries\C
Language Reference\Declarations and Types\C Storage Classes\Storage-Class
Specifiers for External-Level Declarations (whoo, that's a mouthful!)

"The rules for using extern are:

* The extern storage-class specifier declares a reference to a variable
defined elsewhere. You can use an extern declaration to make a definition in
another source file visible, or to make a variable visible prior to its
definition in the same source file. Once you have declared a reference to
the variable at the external level, the variable is visible throughout the
remainder of the translation unit in which the declared reference occurs.

* For an extern reference to be valid, the variable it refers to must be
defined once, and only once, at the external level. This definition (without
the extern storage class) can be in any of the translation units that make
up the program. "

There's one subtopic under that topic, labeled Example, which shows how to
do it--although it does NOT show the using-a-header-file or macro techniques
we've mentioned here.



Sat, 05 Jan 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How can I use/share global variables and objects in an ASP.NET application

2. LNK2005 - Sharing global variables

3. Shared global variables

4. Global variable or not global variable

5. Global Variables in Shared Objects

6. global variable shared by several files

7. Sharing a global variable in a DLL

8. Uninitialized global data (was Re: Global variables)

9. static local variables vs global variables

10. Variables & Global Variables

11. global variables and external variables?

12. Sharing Global Data

 

 
Powered by phpBB® Forum Software