Search code examples
c++directxdirectx-10

"Extern" with DirectX variables?


For some reason, whenever I declare a directx variable as extern, I receive a linking error.

Example:

In some header file:

extern ID3D10EffectMatrixVariable* pWorldVariable;

In some other cpp file where I include the .h file containing pd3dDevice:

pWorldVariable = NULL;

An error similar to this will pop up:

2>main.obj : error LNK2001: unresolved external symbol "struct ID3D10EffectMatrixVariable * pProjectionVariable" (?pProjectionVariable@@3PAUID3D10EffectMatrixVariable@@A) 2>C:\Users\steve\documents\visual studio 2010\Projects\Shyr\Debug\Shyr.exe : fatal error LNK1120: 1 unresolved externals

The minute I take away the extern declaration, it compiles like a charm. Of course, I'm actually wanting to reference several variables, such as my swap chain, device, target view, etc from a dll I'm working on. Anybody know what's up?

(Also, YES, it only declared once)

Just to prove that the issue is isolated to DirectX variables, I made an extern variable "george" and initialized it to 4. I then referenced it elsewhere and changed the value. Compiled just fine.


Solution

  • The issue I was running across was a matter of not understanding extern declaration vs. definition. This answer helps, but you have to read the whole thing and then do some research to understand it:

    What is the difference between a definition and a declaration?