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 :slight_smile:

Regards,
Tane Piper
http://tane.cream.org

try this for all your header files, heres an example for globals.h:

#ifndef GLOBALS_H
#define GLOBALS_H 1

//your stuff goes here

#endif

this makes it so each file is only included once, so you dont get the
multiple definition errors.> ----- Original Message -----

From: tane.piper@ukonline.co.uk (Tane Piper)
To:
Sent: Tuesday, March 04, 2003 3:20 PM
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 :slight_smile:

Regards,
Tane Piper
http://tane.cream.org


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Erhm. Been a while, but try marking it as ‘extern’ in the .h file.

I’m sure someone else will have a more thorough answer for you, but
in the meantime, I hope this helps some :^)

-bill!

(Still searching for a new job!)On Tue, Mar 04, 2003 at 11:20:47PM -0000, Tane Piper wrote:

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


bill at newbreedsoftware.com Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/

Do both.

Do the #ifdef/#define/#endif suggested by Atrix AND do the extern thing. I
used to mess this stuff up all the time, but since I’ve been using the
following setup (stolen from a CDX example :slight_smile: things have been just fine:

In your .h file add:

#ifdef MAKE_EXTERN
#define DECLARE
#else
#define DECLARE extern
#endif

Then below that do some:

DECLARE screen;
DECLARE somethingglobal;
DECLARE goods;
DECLARE whatever;

Then at the very top of -one- of your .cpp files (before #include-ing) you add:

#define MAKE_EXTERN

Works like a charm :slight_smile:

/Fredrik

At 16:00 2003-03-04 -0800, you wrote:>On Tue, Mar 04, 2003 at 11:20:47PM -0000, Tane Piper wrote:

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

Erhm. Been a while, but try marking it as ‘extern’ in the .h file.

I’m sure someone else will have a more thorough answer for you, but
in the meantime, I hope this helps some :^)

-bill!

(Still searching for a new job!)


bill at newbreedsoftware.com Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

yeah do that, looks like he knows what hes doin more than me (:

in fact i think im gonna start doin it that way too :stuck_out_tongue:

thanks for the tip Fredrik> ----- Original Message -----

From: azaka@gmx.net (Fredrik)
To:
Sent: Tuesday, March 04, 2003 4:24 PM
Subject: Re: [SDL] General header question

Do both.

Do the #ifdef/#define/#endif suggested by Atrix AND do the extern thing. I
used to mess this stuff up all the time, but since I’ve been using the
following setup (stolen from a CDX example :slight_smile: things have been just fine:

In your .h file add:

#ifdef MAKE_EXTERN
#define DECLARE
#else
#define DECLARE extern
#endif

Then below that do some:

DECLARE screen;
DECLARE somethingglobal;
DECLARE goods;
DECLARE whatever;

Then at the very top of -one- of your .cpp files (before #include-ing) you
add:

#define MAKE_EXTERN

Works like a charm :slight_smile:

/Fredrik

At 16:00 2003-03-04 -0800, you wrote:

On Tue, Mar 04, 2003 at 11:20:47PM -0000, Tane Piper wrote:

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

Erhm. Been a while, but try marking it as ‘extern’ in the .h file.

I’m sure someone else will have a more thorough answer for you, but
in the meantime, I hope this helps some :^)

-bill!

(Still searching for a new job!)


bill at newbreedsoftware.com Hire
me!

http://newbreedsoftware.com/bill/
http://newbreedsoftware.com/bill/resume/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Fredrik wrote:

Do both.

Do the #ifdef/#define/#endif suggested by Atrix AND do the extern
thing. I used to mess this stuff up all the time, but since I’ve been
using the following setup (stolen from a CDX example :slight_smile: things have
been just fine:

I also use the method of #ifndef and extern combined, but somewhat
different. Something like the following:

#ifndef HEADERFILE_H
#define HEADERFILE_H
extern int var;
#endif

Since I use this method I don’t remenber of having any error involving
multiple defenitions.
But don’t ever forget that the extern only means that the var is defined
somewhere else, but IT IS defined. I’m just telling this because I’ve
seen people before thinking that the extern defined the vars, and they
wouldn’t need to define it in another place. So what you can do is have
all your global variables defined for example in something like the
"main.cpp" and whenever you need it just use the extern, telling you’re
using the var, but it’s defined in another place!
I hope I got myself clear enought.

And remember to make everything including #includes in the include file
inside the #ifndef and #endif pair.
This will prevent circular dependencies.On Wednesday 05 March 2003 02:03, Atrix Wolfe wrote:

try this for all your header files, heres an example for globals.h:

#ifndef GLOBALS_H
#define GLOBALS_H 1

//your stuff goes here

#endif

this makes it so each file is only included once, so you dont get the
multiple definition errors.

Has somebody tried that ?

I’m working with Linux and X11.
I made some tries to use SDL in a X window and it works fine.
But I can’t find informations about how to control that window (location,
hiding borders, …).

Any suggestions ?

Richard Nav?os

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@ukonline.co.uk (Tane Piper)
To:
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 :slight_smile:

Regards,
Tane Piper
http://tane.cream.org


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

http://www.libsdl.org/cvs/qtSDL.tar.gz> ----- Original Message -----

From: rnaveos@kontronmedical.ch (Richard NAVEOS)
To:
Sent: Wednesday, March 05, 2003 9:30 AM
Subject: [SDL] Displaying SDL surfaces in a QT widget

Has somebody tried that ?

I’m working with Linux and X11.
I made some tries to use SDL in a X window and it works fine.
But I can’t find informations about how to control that window (location,
hiding borders, …).

Any suggestions ?

Richard Nav?os


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Just to be a stick in the mud…

Ask yourself - “Do you really need global vars” anyways?

They are a maintenance nightmare. Most code can be written
without using them. Just pass what you need in and out of
functions. If this would mean too many parameters, send the
params wrapped up in a structure - something along the lines of!

typedef struct ProgVars
{
int Width, Height… etc, etc
} ProgVars_t;

void function(ProgVars_t *ProgVars)
{
ProgVars->Width = ???;
}

int main(int argc, char **argv)
{
ProgVars_t MyVars;

function(&MyVars);

}

yo guys, this info was helpful, but its veering away from SDL, does everyone
have to say everything even remotely relevant to headers or can we give it
up and go back to sdl talk?> ----- Original Message -----

From: steven.cullum@eds.com (Cullum, Steve)
To:
Sent: Wednesday, March 05, 2003 2:15 AM
Subject: RE: [SDL] General header question

Just to be a stick in the mud…

Ask yourself - “Do you really need global vars” anyways?

They are a maintenance nightmare. Most code can be written
without using them. Just pass what you need in and out of
functions. If this would mean too many parameters, send the
params wrapped up in a structure - something along the lines of!

typedef struct ProgVars
{
int Width, Height… etc, etc
} ProgVars_t;

void function(ProgVars_t *ProgVars)
{
ProgVars->Width = ???;
}

int main(int argc, char **argv)
{
ProgVars_t MyVars;

function(&MyVars);
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl