#### Archived

This topic is now archived and is closed to further replies.

# Files

This topic is 6092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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
#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

##### Share on other sites

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?

##### Share on other sites
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

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

##### Share on other sites
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?

##### Share on other sites
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:

struct global_stuff{   int number;   char letter;} ;extern global_stuff globals;

hope that helps.

dave

--
david@neonstar.net
neonstar entertainment

##### Share on other sites
Thanks for your help neonstar, it all works now

##### Share on other sites
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

##### Share on other sites
Teej had a nice little trick:
#ifndef GLOBALS_OWNERSHIPextern#endifglobal_stuff globals;

Then in the file you want to own the globals structure:
#define GLOBALS_OWNERSHIP

##### Share on other sites
There is an extremely simple solution to this problem, you know...

"A society without religion is like a crazed psychopath without a loaded .45"

• 11
• 20
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999847
×