Advertisement

I feel stupid for asking this but...

Started by May 05, 2001 08:55 PM
4 comments, last by Kranomano 23 years, 9 months ago
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.
Advertisement
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:

  //** Header file#ifndef HEADER_H#define HEADER_Htypedef struct somestruct_tag {   // create vars in here} somestruct;extern somestruct instance;#endif  


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!
[Cyberdrek | ]
Well, declare the structure in a headers, like so:
  // DataHolder.H#pragma once#ifndef INC_DATAHOLDER_H // Inclusion Guards#define INC_DATAHOLDER_Htypedef struct {  int Something;} DataHolder;#endif  

Then, have another header file that is used for your globals. This is the one included in other files.
  // Globals.h#pragma once#ifndef INC_GLOBAL_H#define INC_GLOBAL_H#include "DataHolder.h"extern DataHolder Holder;#endif  

Then, have the file that Global.h is refering to. This one should be included in your project.
  // Globals.cpp#include "Globals.h"DataHolder Holder;  


Resist Windows XP''s Invasive Production Activation Technology!
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:
  // extern_globals.h#include " mystruct.h "#ifndef GLOBALS_EXTERN_H#define GLOBALS_EXTERN_Hextern my_struct system_info;#endif  


And heres the "real" globals file:
  // globals.h#include " mystruct.h "#ifndef GLOBALS_H#define GLOBALS_Hmy_struct system_info;#endif  


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.
Oops, I was just using extern wrong. Thanks guys!

This topic is closed to new replies.

Advertisement