[SDL] stupid and off-topic (sorry)

Karsten Laux klaux at student.uni-kl.de
Fri Aug 27 18:27:24 PDT 1999


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. :)

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



More information about the SDL mailing list