Embarrasing question about Globals
Hello all,
I have been programming long enough that I thought I should know the answer to this one, but can''t seem to work it out. I am working through Game Programming for Dummies and typing in some of the demos by hand. What I want to do is split the functions up into different files. I created one header file just for globals, but when I add it to my .cpp files I get linker errors saying the variables have been previously defined.
As much as global variables are used in game programming I am sure there is a way around this. Any help is greatly appreciated.
Get into the habit of writing header files that look something like this (assume that the header file in question is called globs.h):
#ifndef GLOBS_H
#define GLOBS_H
//all your defs, etc.
#endif
That way, anything in the header file will only be defined once, no matter how many times the file is included.
[edited by - Plasmadog on January 7, 2003 8:56:15 PM]
#ifndef GLOBS_H
#define GLOBS_H
//all your defs, etc.
#endif
That way, anything in the header file will only be defined once, no matter how many times the file is included.
[edited by - Plasmadog on January 7, 2003 8:56:15 PM]
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
If you''re using Visual C++, look at using
#pragma once
to keep the compiler from having to open the include file each time and parse the #ifdef and then skip over it. For large projects this can speed up your builds quite a bit.
#pragma once
to keep the compiler from having to open the include file each time and parse the #ifdef and then skip over it. For large projects this can speed up your builds quite a bit.
quote: Original post by Plasmadog
Get into the habit of writing header files that look something like this (assume that the header file in question is called globs.h):
#ifndef GLOBS_H
#define GLOBS_H
//all your defs, etc.
#endif
That way, anything in the header file will only be defined once, no matter how many times the file is included.
I'm not sure that would cut it - I believe it would be included once per compilation unit , which would still cause a conflict at the linking stage. (You should still use these so-called include guards, as they help you get rid of redeclaring errors, but the redefining errors will persist, provided your project consists of more than one source file.)
What you really want to do is define all global variables in a source file (say, globals.cpp). In order for them to be declared in each compilation unit, you can declare them (as extern) in the global header (global.hpp), which you then include in all the files that need it.
(Actually, what you really want to do is to avoid global variables: They are usually considered a Bad Thing. If you necessarily have to use them, though, I believe this is the best way.)
[edited by - Miserable on January 7, 2003 9:06:06 PM]
January 07, 2003 08:05 PM
Use extern in the .h files e.g
extern int x;
extern char *spiceyhorse;
And declare the globals once in a .cpp file.
This may not be useful.
extern int x;
extern char *spiceyhorse;
And declare the globals once in a .cpp file.
This may not be useful.
Thank you all very much! Your solutions were helpful and correct. With a little typing and a quick glance at my C book, Extern did the trick and I am moving forward once again!
I think anonymous poster is right about your problem. You have to put this in a .h file:
#pragma once
extern int globalvariable;
and this in A globals.cpp file:
#include "thatfileabove.h"
int globalvariable;
then include thatfileabove.h in all the files that need your globals...except try a name like globals.h, as it seems to work better lol
brian
#pragma once
extern int globalvariable;
and this in A globals.cpp file:
#include "thatfileabove.h"
int globalvariable;
then include thatfileabove.h in all the files that need your globals...except try a name like globals.h, as it seems to work better lol
brian
Brian J
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement