I feel stupid for asking this but...
I hate to ask this, at risk of making myself seem stupid, but I''ve got a question about global variables. I want to make a structure that holds a lot of system info (hDC, resoultion, key states, etc, etc) accessable from any file. I want to put it in a header file, then include that in all the other files that need the info. I''ve tried everything I could think of, but nothing would work. Declaring it as static obviously doesnt work, using extern causes a rash of LNK2001''s, and.... well, I dont know what to do.
Sorry for being so long winded, I''m just a bit frustrated.
Here''s what you do:
In a header file you define the structure (fx MYSTRUCT). In that same header file you say
extern MYSTRUCT myobject;
In some C/C++ file you declare the object
MYSTRUCT myobject.
Now every sourcefile that includes the header can reach myobject. It''s that easy.
PS: putting static in front of myobject makes it invisible to everything outside the source file, in case you didn''t know.
In a header file you define the structure (fx MYSTRUCT). In that same header file you say
extern MYSTRUCT myobject;
In some C/C++ file you declare the object
MYSTRUCT myobject.
Now every sourcefile that includes the header can reach myobject. It''s that easy.
PS: putting static in front of myobject makes it invisible to everything outside the source file, in case you didn''t know.
It seems pretty simple to me, create your struct in a header file, then, in the same header file, instantiate your struct as extern. ie:
Note that I haven''t tried this code or anything but it should work. It did in a verry old DX3 book that I have. So it should work for you also. Anyhow, I hope it does...
"And that''s the bottom line cause I said so!"
Cyberdrek
Headhunter Soft
A division of DLC Multimedia
|
Note that I haven''t tried this code or anything but it should work. It did in a verry old DX3 book that I have. So it should work for you also. Anyhow, I hope it does...
"And that''s the bottom line cause I said so!"
Cyberdrek
Headhunter Soft
A division of DLC Multimedia
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.cjb.net/Out_Source/resist.jpg)
[Cyberdrek | ]
Well, declare the structure in a headers, like so:
Then, have another header file that is used for your globals. This is the one included in other files.
Then, have the file that Global.h is refering to. This one should be included in your project.
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
|
Then, have another header file that is used for your globals. This is the one included in other files.
|
Then, have the file that Global.h is refering to. This one should be included in your project.
|
![Resist Windows XP''s Invasive Production Activation Technology!](http://druidgames.warfactory.com/Out_Source/resist.jpg)
http://druidgames.cjb.net/
What I do is make two files: One holds the extern declarations of the global variables, and one holds the actual declarations.
(All my declarations for custom data types like structures are in their own files that are included into the globals and extern globals files).
Like this:
And heres the "real" globals file:
Now this is how you set it up. You can include extern_globals.h in any number of files that need access to system_info . The globals.h file is included only once , and in your main source file, most likely the one that has main(), etc.
(All my declarations for custom data types like structures are in their own files that are included into the globals and extern globals files).
Like this:
|
And heres the "real" globals file:
|
Now this is how you set it up. You can include extern_globals.h in any number of files that need access to system_info . The globals.h file is included only once , and in your main source file, most likely the one that has main(), etc.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement