# Using keyword extern...

This topic is 4350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my project, I have a few engines (particleEngine, cutsceneEngine, inputEngine etc...). For each one, there is only once instance; however, there are times when (lets say) the graphicsEngine needs to be aware of the particleEngine. So what I did was make a .h and .cpp file for every "engine" (only about 6) and then I would put: #include "GlobalEngineNeeded.h" and each GlobalEngineNeeded.h would look something like this:
#ifndef EG_GlobalEngineNeeded
#define EG_GlobalEngineNeeded

#include "EngineNeeded.h"

extern EngineNeeded *instanceOfThisEngine;

#endif


and each GlobalEngineNeeded.cpp would like something like this:
#include "GlobalEngineNeeded.h"

EngineNeeded *instanceOfThisEngine = new EngineNeeded();


Then, In each Engine that I would need another engine, I would do something like this (using graphics engine as example):
#include "GlobalParticleEngine.h"
#include "GlobalLightingEngine.h"

class GraphicsEngine
{ //instanceOfParticleEngine and instanceOfLightingEngine are used somewhere in here
.
.
.
};


Now here's the problem...All of this compiles (no compiler or linker error), but as SOON as I run my program, I get a runtime error (win32 programs are TERRIBLE at giving tips about runtime errors). I KNOW that it has to do with the way that I'm using these global variables. The reason I know this is b/c the runtime error comes even before my window is created, meaning it would have something to do with my .h files(please just take my word for it). Am I doing something wrong here? If there is a simple reason as to why this error is occurring, please let me know. Otherwise (and ONLY if you don't know how to fix my code), could someone advise me as to what they would do?

##### Share on other sites
I would run the program with a debugger attached. This way, the debugger would tell me on what line the runtime error occurs, and what the current state of all objects is.

My guess is that your order of initialization is broken (that is, a global element is used before it is initialized). Of course, I assume that there are no other mistakes in your code which may cause this.

##### Share on other sites
Your use of 'extern' is just fine. The runtime error must lie elsewhere because external variables are a purely C++ abstraction.

Once the linker has resolved and okayed the external, it is thrown into the .data pool along with all the public and initialised local variables. Once your PE has been compiled and linked, all your object files end up in the .text/.code section and the external variables in .data - the public variables are almost indistinguishable from the external ones.

Regards

##### Share on other sites
The order of initialization between variables in different files is not defined. It is likely that one engine is trying to use another engine before it has been initialized.

One solution is to use Singletons (taking advantage of the lazy instantiation feature). Though, you still need to be careful of circular dependencies in the constructors.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 22
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631732
• Total Posts
3001926
×