I posted a question about global variable declaration and externing in this thread yesterday (you don't need to read it):
http://www.gamedev.net/community/forums/topic.asp?topic_id=580798
Basically, I have been making the transition from standard C to C++ over the past few days and was pulling my hair out over some of the differences between the two languages. Specifically, I was very annoyed at the fact that declaring global variables in C++ is not as simple as it is in C.
In standard C, you can simply declare your global variables in your program's main header (assuming you use one) and leave it at that. Depending on which compiler you use and the parameters you pass to it, C++ handles it in a different manner. In most cases, a C++ compiler will complain when it encounters a variable that has already been declared in a particular scope, which happens if you include a main header in your entry point .cpp file that lists your global variables.
Solving this problem is usually either an annoying case of externing every variable whenever need to use it in a different file or by including a header file in your source code that lists all of the externs to global variables that have been declared in your program. Some compilers can get around this annoying hastle depending on the way they put the program together and the parameters you pass to it, but this is not really necessary as I will demonstrate.
In either case, you need to type more than you should, and in an ideal world, you would only need to declare your global variables once and leave it at that.
After tinkering around for a little while, I figured out a very simple way of doing this:
1) Before you include your main header file in your entry point file, insert this:
#define GLOBAL
From there, you would insert your main header file as usual:
#include "game.hpp"
2) Once you have that define in place, open your main header file or the header file where you declare your global variables (I use one called "data.hpp" which is included in my game's main header, for instance). Insert the following code before you declare your global variables:
#ifndef GLOBAL#define DATA extern#else#undef GLOBAL#undef DATA#define DATA#endif
From there, all you simply need to do is prefix all of your global variable declarations with DATA (or whatever term you want to use). For example:
DATA bool active;DATA string title;DATA Game* game;
And that's it! Now you don't need to manually extern everything, which saves a lot of work.
To explain how it works, basically the compiler will understand that it needs to declare variables as data for your program's entry point file and use them as externs for everything else. To the compiler, the entry point of the application is telling it that DATA represents white space, which is exactly the same as declaring a variable as you normally would. Every other source code file in the program interprets DATA as extern, so your global variables are only declared once.
I hope this is helpful to you guys!
- Sig