Hi.
I was just wondering how I should go about splitting my project up in several sourcefiles and use only one headerfile with all the globals.
i know i should use #ifndef and stuff but im not really clear on this
any help is appriciated
if you want to put all of your globals in one header and use that header in your multiple source files, then you will want to use inclusion guards in the global header. yeah, you are on the right track with the ifndefs. here's an example
global.h
what this does is check and see if _GLOBAL_H is defined, and if it's not it defines it and defines the rest of the variables and functions. if _GLOBAL_H is already defined, the variables aren't declared. you don't necessarily have to use the beginning underscore, but it just helps you make sure that there isn't already some other #define called that. not necessarily in your code, but in library headers and such.
anyway, this should be what you need.
dave
--
david@neonstar.net
neonstar entertainment
Edited by - neonstar on October 16, 2001 4:03:28 PM
global.h
#ifndef _GLOBAL_H#define _GLOBAL_H// define your functions and variables#endif
what this does is check and see if _GLOBAL_H is defined, and if it's not it defines it and defines the rest of the variables and functions. if _GLOBAL_H is already defined, the variables aren't declared. you don't necessarily have to use the beginning underscore, but it just helps you make sure that there isn't already some other #define called that. not necessarily in your code, but in library headers and such.
anyway, this should be what you need.
dave
--
david@neonstar.net
neonstar entertainment
Edited by - neonstar on October 16, 2001 4:03:28 PM
Thanks for your reply neonstar
i did exactly what you showed but it still give me errors:
error LNK2005: "blah blah" already defined in WinMain.obj
any ide what could be wrong?
i did exactly what you showed but it still give me errors:
error LNK2005: "blah blah" already defined in WinMain.obj
any ide what could be wrong?
what i gave you there should have prevented from that problem. just remember to keep the #endif in the header file after all of your variable and function declarations and the #ifndef and #define before them.
ah, i just thought of something. some variables you have to define as extern in the header file, then define again in the main source file they will be used in. for example:
in global.h
and in main.cpp
you won''t have to define it again in every source file. just define it in one and it will be fine. function declarations will be fine, but variables are funny in that respect. let me know if you have more problems or don''t understand what I did here.
dave
--
david@neonstar.net
neonstar entertainment
ah, i just thought of something. some variables you have to define as extern in the header file, then define again in the main source file they will be used in. for example:
in global.h
extern char ProgramName[100];
and in main.cpp
char ProgramName[100];
you won''t have to define it again in every source file. just define it in one and it will be fine. function declarations will be fine, but variables are funny in that respect. let me know if you have more problems or don''t understand what I did here.
dave
--
david@neonstar.net
neonstar entertainment
Hi
thanks again
one more question tho
would it be possible to put all the global variables in a struct
and then make the struct itself extern?
thanks again
one more question tho
would it be possible to put all the global variables in a struct
and then make the struct itself extern?
yeah, you can put then all in a struct, but don''t make the struct declaration extern. when you define a variable from the struct, then you should extern. like so:
hope that helps.
dave
--
david@neonstar.net
neonstar entertainment
struct global_stuff{ int number; char letter;} ;extern global_stuff globals;
hope that helps.
dave
--
david@neonstar.net
neonstar entertainment
hey, no problem. moving your globals around can really be a pain in the ass and i''ve had my share of problems with them. just trying to share the info
dave
dave
Teej had a nice little trick:
Then in the file you want to own the globals structure:
#ifndef GLOBALS_OWNERSHIPextern#endifglobal_stuff globals;
Then in the file you want to own the globals structure:
#define GLOBALS_OWNERSHIP
There is an extremely simple solution to this problem, you know...
"A society without religion is like a crazed psychopath without a loaded .45"
"A society without religion is like a crazed psychopath without a loaded .45"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement