• Advertisement
Sign in to follow this  

Globals [RESOLVED]

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yes, I know their evil, but I need them. I have 2 files that need to access a SDL_Surface named screen. They both include "Globals.h" and both have the using namespace std; expression. So, in the globals header, I have extern SDL_Surface *screen. In the main.cpp file, where my main stuff is, right after #include "Globals.h", I have SDL_Surface *screen = NULL. In my graphics functions file, I have SDL_Surface *screen. So I'm getting the linker errors: --------------------Configuration: MGSDL - Win32 Debug-------------------- Compiling... GameFunctions.cpp Linking... GameFunctions.obj : error LNK2005: "struct SDL_Surface * screen" (?screen@@3PAUSDL_Surface@@A) already defined in Main.obj Debug/MGSDL.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. MGSDL.exe - 2 error(s), 0 warning(s) What am I doing wrong? Globals are confusing...(if there's a better solution to having multiple files access the same variables, please post it!) [Edited by - orcfan32 on May 8, 2006 1:02:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
are you using?

#ifndef GLOBALS_H
#define GLOBALS_H

/// header file stuff here....

#endif


to wrap your header file?

theTroll

Share this post


Link to post
Share on other sites
Main.cpp:
- SOURCE REMOVED -

Globals.h:
- SOURCE REMOVED -

And GameFunctions.cpp (Be warned that this is incomplete, while I was waiting for replies, I was working on the function in here):
- SOURCE REMOVED -

(And yes, I'm aware of the TTF errors; ignore them.)

[Edited by - orcfan32 on May 8, 2006 12:14:39 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look at your linking error:

GameFunctions.obj : error LNK2005: "struct SDL_Surface * screen" (?screen@@3PAUSDL_Surface@@A) already defined in Main.obj


This look at your main.cpp and your GameFunctions.cpp .. you've defined screen in BOTH, which is exactly what your linker error is saying. Take out the define in your GameFunctions.cpp

Share this post


Link to post
Share on other sites
Only declare the variable in the header and in any other cpp that needs it use extern

extern SDL_Surface *screen;

Cheers
Chris

Share this post


Link to post
Share on other sites
As an alternative idea, you could create something along the lines of:

class SDLApplication
{
private:
SDL_Surface* mScreen;

...

public:
bool Resize( int width, int height );

...
};


This would allow you to encapsulate initializing and cleaning up after SDL.

Share this post


Link to post
Share on other sites
Oh, I thought that you had to declare it in each source file you used it in.. So I should just have a Globals.cpp declaring the globals. Thanks, you helped a lot.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement