Not defined in scope... multiple definitions

I'm using Code Blocks and SDL.

Yes I am fairly new to C++, but I do have a basic understanding to it.

Reason: In efforts to clean my project up. I've made multiple source files etc. When I have all my code in 1 source file and I compile the heavens open up and a light is cast on me with an angelic choir singing. But there's a million lines of code and navigating is damn near impossible.

Problem: So after I split up the code amongst the source files. I need to call on the variable in multiple source files.

SDL_Surface *screen = NULL;

Thus putting it in 1 source file and not the other gives me this.

'screen' was not declared in this scope|

Putting it in a header file thus including the header to each source. Gives me this.

'SDL_Surface* screen' previously defined here|

Understanding: Now from reading around before I posted this. I learned(or believe I have) that each time a header is included that it recreates these varibles thus giving me the "previously defined" error. And placing SDL_Surface *screen = NULL; in each file obviously does the same.

Questions: How do I use the variable amongst the multiple sources without redefining? Or what am I doing wrong? Is this an SDL issue? Please help before I take out my frustration on inanimate objects.
It has nothing to do with SDL. If you want to use a global variable in different translation units, you need to declare it in each (usually by putting the declaration in the appropriate header and including it):
extern SDL_Surface* screen;

Now there needs to be exactly one translation unit (.cpp file) that contains the definition.
Last edited on
THANK YOU! I actually came across someone saying use extern in other topics. But for some reason the way you explained it clicked. You sir are by far the hero of my day. Help yourself to the beer and christmas cookies in my fridge. :D
Topic archived. No new replies allowed.