# Redefinitions, even with #ifndef (Followup question)

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

## Recommended Posts

I have a tetris clone I'm working on, with several header files (game.h, player.h, block.h, etc...). Some of these have to share variables inbetween them, for example; draw.h gets info from game, player and block to draw the screen. I'm basicly using the same format on all my header files: #ifndef DRAW_H #define DRAW_H #include game.h #include player.h #include block.h void drawFunctions(); type variables; #endif But I'm still getting hundreds of redefinition errors. Is there anything I might have overlooked? Can I use extern to resolve this, and how? Thanks for any help on this, Ron (sorry if this might seem like a repost, I figured the topic changed enough to start again) [Edited by - ronaldo on April 30, 2006 1:49:41 PM]

##### Share on other sites
in a header file

extern type variableName;

In exactly one source file, have a line that reads

type variableName;

The extern bit tells the compiler/linker that the variable is to be found elsewhere.

Note, globals, particularly shared globals, are almost always a bad design idea and can almost always be refactored away.

##### Share on other sites
Yes, if you are defining the varibles in header files and using the header files in more than one source file (.c, .cpp) then the compiler will generate multiple copies of the varible.

All you have to do is put extern infront in the header definition -
// myHeader.h#ifndef MYHEADER_H#define MYHEADER_Hextern int MyInt;#endif

Then in one source file that includes the header file, define the varible -
// mySourceFile.cpp#include "myHeader.h"int MyInt;

Then you can include the header as many times as you want in other source files and they will all use the varible defined in the source file, preventing multiple definitions.

Edit: Lost the posting race by a minute :(

[Edited by - Moomin on April 30, 2006 12:47:55 PM]

##### Share on other sites
// foo.h#ifndef FOO_H#define FOO_Hextern int foo;#endif // FOO_H// foo.cpp#include "foo.h"int foo;

i should have known others would answer faster.

##### Share on other sites
If you have a lot of globals, and they're all in the same place, and you want them to be externed all over the place, you can do something like this:

(note: this is really not a 'nice' solution, but if you can put your globals into one place, you can easily forget about all the extern stuff and go on your merry way).

// somewhere in globals.h#ifdef NOT_EXTERN#define GLOBALDEF(a, b) a = b#define EXTERN#else // NOT_EXTERN#define GLOBALDEF(a, b) extern a#define EXTERN extern#endif // NOT_EXTERN// ...crap...GLOBALDEF(GameState &gameState, GameState::getInstance());GLOBALDEF(int curFrameTimeStart, 0);EXTERN Player player1;EXTERN Player player2;// ONE file that includes this...#define NOT_EXTERN#include "globals.h"#undef NOT_EXTERN// All other files that include this...#include "globals.h"

##### Share on other sites
Thanks for the help, I have a quick follow up question :D

Should I use extrn for function and class definitions in my header files as well as the variables? What about variables declared within the class definitions?

Thanks a million!
Ron :D

##### Share on other sites

#define GLOBALDEF(a, b) a = b
#define EXTERN
#else // NOT_EXTERN
#define GLOBALDEF(a, b) extern a
#define EXTERN extern
#endif // NOT_EXTERN

// ...crap...

GLOBALDEF(GameState &gameState, GameState::getInstance());
...

Are you using a singleton and then making it global?

##### Share on other sites
Quote:
 Original post by Anonymous Poster#define GLOBALDEF(a, b) a = b#define EXTERN#else // NOT_EXTERN#define GLOBALDEF(a, b) extern a#define EXTERN extern#endif // NOT_EXTERN// ...crap...GLOBALDEF(GameState &gameState, GameState::getInstance());...Are you using a singleton and then making it global?

I'm just showing that you could, if you really wanted to. I wanted to put some function call into the 'b' field, and that was the first thought that came to mind.

##### Share on other sites
I found that with a clean and rebuild solution, the errors were eliminated.
Thanks for the sugestions guys!
Ron

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
gaxio
10

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002310
×