Stupid and off-topic (sorry)

Sam Lantinga wrote:

When including this header file in more than one .cc I get multiple
defines of the variables define in this header. I think the #ifndef
should take care of this problem, but it doesn’t. Why?

Thanks for any help.


#ifndef
_MAIN_H
#define
_MAIN_H

should be:
#ifndef _MAIN_H
#define _MAIN_H

It’s like that. My mailer did the line break.

Any other suggestion?

Karl Bartel wrote:

When including this header file in more than one .cc I get multiple
defines of the variables define in this header. I think the #ifndef
should take care of this problem, but it doesn’t. Why?

Thanks for any help.


#ifndef
_MAIN_H
#define
_MAIN_H

#include
"PowerPak/powerdraw.h"
#include
"PowerPak/powerpak.h"

int SIDE=100; //The width of the not scrolling place at the
right
int START=100; //for function whowins, hard to
explain…
int STOP=2; //the speed is devided by this factor when you hit a
wall
int xlen,ylen; //Screen width and
heigth

#endif

your #ifndef stuff is perfectly correct. But any #defines are only valid
in the same scope and this is fine if you deal with variable or class
declartions.
But you define variables here, that is the problem.
Each source module which includes this header file will contain the
variables SIDE, START, etc and thus the linker does not know which to
choose and reports about multiple defines.

You should usually avoid to define variables in header files, declare
some only using “extern” and create a .c module which really defines
them:

foo.h--------------
#ifndef FOO_H
#define FOO_H

//only declaration here

extern int SIDE;
extern int START;

#endif

foo.c

#include “foo.h”

//definition and initialization here !

int SIDE = 100;
int START = 100;

I hope I could help you. :slight_smile:


Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux
UIN 21614933 (Bert)

Thanks alot! This problem was driving me mad.

Karsten Laux wrote:>

Karl Bartel wrote:

When including this header file in more than one .cc I get multiple
defines of the variables define in this header. I think the #ifndef
should take care of this problem, but it doesn’t. Why?

Thanks for any help.


#ifndef
_MAIN_H
#define
_MAIN_H

#include
"PowerPak/powerdraw.h"
#include
"PowerPak/powerpak.h"

int SIDE=100; //The width of the not scrolling place at the
right
int START=100; //for function whowins, hard to
explain…
int STOP=2; //the speed is devided by this factor when you hit a
wall
int xlen,ylen; //Screen width and
heigth

#endif

your #ifndef stuff is perfectly correct. But any #defines are only valid
in the same scope and this is fine if you deal with variable or class
declartions.
But you define variables here, that is the problem.
Each source module which includes this header file will contain the
variables SIDE, START, etc and thus the linker does not know which to
choose and reports about multiple defines.

You should usually avoid to define variables in header files, declare
some only using “extern” and create a .c module which really defines
them:

foo.h

#ifndef FOO_H
#define FOO_H

//only declaration here

extern int SIDE;
extern int START;

#endif

foo.c

#include “foo.h”

//definition and initialization here !

int SIDE = 100;
int START = 100;

I hope I could help you. :slight_smile:


Karsten-O. Laux
klaux at student.uni-kl.de
http://www.rhrk.uni-kl.de/~klaux
UIN 21614933 (Bert)