Globals [RESOLVED]

Started by
10 comments, last by JohnBolton 17 years, 11 months ago
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]
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Advertisement
are you using?

#ifndef GLOBALS_H
#define GLOBALS_H

/// header file stuff here....

#endif


to wrap your header file?

theTroll
Yup. I made sure to protect that stuff like that but it's still not working. :/
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Rip the code down to just the basics and show us. I think we can figure it out.

theTroll
Globals aren't always a bad thing......it just depends on what you're using them for. In this case I think using them would be ok.
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]
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
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
Only declare the variable in the header and in any other cpp that needs it use extern

extern SDL_Surface *screen;

Cheers
Chris
CheersChris
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.
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.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit

This topic is closed to new replies.

Advertisement