An extern variable in C++ has to have the following setup:
- A declaration that will be visible to any module that wants to access that variable. This comes in the form:
extern int g_iMyExternVariable; // Note that you cannot initialize the extern's value here
- The definition that describes which module will actually hold the data for the real object. This has to be local for a single CPP file only, as you only want to create a single object. This comes in the form:
int g_iMyExternVariable = 24; // Here you actually allocate the data and initialize it to some value.
In your case, you have the first requirement met in the cornwall.h header. However, the second one is missing, and that's why the linker is telling you it can't find the actual definition of the variable. Adding this line at the top of cornwall.cpp solves the issue:
RendererContext myRendererContext;
Now, one more note, a RendererContext object is actually of type CRef, so in order to fill it with the proper reference to the renderer context, you just do:
myRendererContext = in_ctxt;
instead of:
RendererContext myRendererContext(in_ctxt);
(which was only declaring a local variable in the scope of the containing function).
Wessam Bahnassi
Microsoft DirectX MVP,
Programmer
Electronic Arts
--
'Talk is cheap because supply exceeds demand'
So I am playing with the new Renderer API and am having trouble storing the RendererContext globally and accessing it in another .cpp file. I first declare this variable in an .h file to be included in other .cpp files. In one of the required callbacks I attempt to define this variable by passing it the context object that is being passed into the callback. I then attempt to use that variable in my other .cpp file. What I end up getting is external linking errors. I have attached my project and am hoping someone could look at the code and share some insight as to why I can't get this to work. I have a feeling it is a problem with my understanding of the 'extern' keyword, I am probably not using it correctly hence my linking errors.
Thanks
Steven