• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

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

# ?

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

## Recommended Posts

to get more organized i split up my code so that i have: main.cpp globals.h functions.h i want both main.cpp and functions.h to use the globals.h variables (globals.h stores constant global vars) but i get errors saying that all vars in globals.h are trying to be declared twice sine i have #include "globals.h" in two places. how do i solve this?

##### Share on other sites
declare the globals as "extern" in the header, then define it in one source file.

for example:

Source.cpp:
int Code;

Source.h:
extern int Code;

##### Share on other sites
At the top of the globals.h file, you need to use some preprocessor directives. You would probably put something like:

#ifndef _GLOBALS_H_
#define _GLOBALS_H_

Then, at the very bottom of the globals.h file, enter the following:

#endif

Now, you can replace _GLOBALS_H_ with anything you want; this is simply a suggestion. However, whatever you change it to, they must be identical on both the #ifndef and the #define lines.

Basically, the first time the compiler (actually, the preprocessor, but we''re trying to make this easy) encounters the #ifndef line, _GLOBALS_H_ will not be defined, and will then proceed to have it defined with the #define line, and include the code between the #define line and the #endif line. Any time the compiler encounters that line (in any file) it will skip all the code until it encounters the matching #endif line.

I would suggest searching for preprocessor directives. There''s several more, and they can be pretty handy.