Redefinitions, even with #ifndef (Followup question)

Started by
7 comments, last by freelancemanga 17 years, 11 months ago
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]
Advertisement
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.
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]
// 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.
This space for rent.
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"
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

#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?
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.
I found that with a clean and rebuild solution, the errors were eliminated.
Thanks for the sugestions guys!
Ron

This topic is closed to new replies.

Advertisement