1

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.

Darkenor
  • 4,349
  • 8
  • 40
  • 67
  • The error message complains about pProjectionVariable, not pWorldVariable. Nevertheless, a *definition* is required. – Hans Passant Apr 03 '11 at 02:38
  • That looks to certainly be the issue. I was switching up so many variables trying to figure out what's wrong that I didn't realize that I copied the wrong error. That said, the error is identical for all the weird directx linking errors. When they're declared and defined, it works. That said - how could you define something like a swap chain externally? It requires zeroing out memory and doing a bunch of other definitions? – Darkenor Apr 03 '11 at 02:51

2 Answers2

2

extern is used to declare a sort of reference to a variable that is used externally across source files (i.e. You declare the variable normally and in each file you use it you have to declare an internal reference to it using extern)

You have to declare the variable in a source file as well.

Example.cpp

ID3D10EffectMatrixVariable* pWorldVariable = 0;

YetAnotherFile.cpp

extern ID3D10EffectMatrixVariable * pWorldVariable;
Khaled Nassar
  • 884
  • 8
  • 23
  • I don't think that's the issue. I've used extern other ways before and I just tried it by keeping it a standard variable, then referenced it in another file as extern and the error still appeared. – Darkenor Apr 03 '11 at 02:14
  • You should declare the variable in the source file not in the header file. If not, give it a shot. – Khaled Nassar Apr 03 '11 at 02:27
  • 1
    It is not because of DirectX types. DirectX structs are simply user-defined entities, they don't receive any "special" treatment by the linker. – Khaled Nassar Apr 03 '11 at 02:29
0

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?

Community
  • 1
  • 1
Darkenor
  • 4,349
  • 8
  • 40
  • 67