[SDL] General header question

Julien Pauty jpauty at irisa.fr
Wed Mar 5 01:05:02 PST 2003


The main idea is that you are making a compilation for each cpp file. You
are defining a variable in your global.h file, if you include it in more
than one cpp file it would be defined more than one time. You should declare
your global in the cpp file is the most related to them. Next, when you have
to access one global var, you just put an "extern int myvar" statement just
before using the variable. The extern statement tells the compiler that this
is already defined elsewhere. The linker will do the job. If you procede
like this you don't need your global.h anymore.

Example :

main.cpp :

unsigned int var;
main() { ......}

ship.cpp:

void Ship :: amethod() {
......
extern unsigned int var;
.......
}

Putting the "ifndef define endif" precompiler statements is also obligatory
but it solve a slighter different problem. With this you prevent the
multiple times inclusion of a header, and cycling inclusion. Even with this,
if you define a global variable in a header that is used in several cpp
files, the variables will be defined several times. Remember that you do a
compilation for each cpp file, then you link.

In my game I procede like this and I had never any multiple definition
problem.

Hope it helps you

Julien

----- Original Message -----
From: "Tane Piper" <tane.piper at ukonline.co.uk>
To: <sdl at libsdl.org>
Sent: Wednesday, March 05, 2003 12:20 AM
Subject: [SDL] General header question


> Hey peeps,
>
> To start with, a lot of my code was in one cpp file,  but I am now
cleaning
> up a lot of my code, and have run into a problem.
>
> First of all I have my main function in main.cpp where I initialise my
> SDL_Surface mainscreen:
>
> mainscreen=SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_NOFRAME);
>
> I define the surface in globals.h, where I keep all my global and const
> variables that are used through the whole program.  I also have the main
> header files such as SDL.h included in this file.  Now the part I am
> developing at the moment is the editor function, which is located in
> editor.cpp, and I blit the interface to the mainscreen surface
>
> main.cpp has globals.h included in it
> editor.cpp has editor.h included in it
> editor.h has globals.h included in it
> globals.h has editor.h included in it.
>
> When I try to compile, I get an error:
> editor.cpp C:\Dev-Cpp\Tane\Galactic\editor.o(.bss+0x0) multiple definition
> of `mainscreen'
> main.cpp C:\Dev-Cpp\Tane\Galactic\main.o(.bss+0x0) first defined here
>
> It does the same with every other const I have, but since they are all not
> essetial at the moment, I have commented them out.
>
> I am compiling in Dev-C++, and all header files have #ifndef at the top so
> they should not be included twice.  Where am I going wrong?  editor.h
needs
> to have globals.h included so it knows some global variables, and
globals.h
> needs to have editor.h included so it knows the editor functions (main.ccp
> only has globals.h included).  I might be something simple I am looking
> over,  but can anyone help?
>
> Sorry if it's confusing too :)
>
> Regards,
> Tane Piper
> http://tane.cream.org
>
>
>
>
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl





More information about the SDL mailing list