“外部”与 DirectX 变量?
由于某种原因,每当我将 directx 变量声明为 extern 时,我都会收到链接错误。
示例:
在某些头文件中:
extern ID3D10EffectMatrixVariable* pWorldVariable;
在其他一些 cpp 文件中,我包含包含 pd3dDevice 的 .h 文件:
pWorldVariable = NULL;
将弹出与此类似的错误:
2>main.obj:错误 LNK2001:无法解析的外部符号“struct ID3D10EffectMatrixVariable * pProjectionVariable”(?pProjectionVariable@@3PAUID3D10EffectMatrixVariable@@A) 2>C:\Users\steve\documents\visual studio 2010\Projects\Shyr\Debug\Shyr.exe:致命错误 LNK1120:1 个无法解析的外部
当我删除 extern
声明时,它会编译就像一个魅力。当然,我实际上想从我正在处理的 dll 中引用几个变量,例如我的交换链、设备、目标视图等。有人知道怎么回事吗?
(另外,是的,它只声明一次)
为了证明问题与 DirectX 变量无关,我创建了一个外部变量“george”并将其初始化为 4。然后我在其他地方引用它并更改了值。编译得很好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
extern
用于声明对跨源文件在外部使用的变量的一种引用(即,您通常声明该变量,并且在使用它的每个文件中,您必须使用extern
声明对它的内部引用)您还必须在源文件中声明该变量。
示例.cpp
YetAnotherFile.cpp
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 usingextern
)You have to declare the variable in a source file as well.
Example.cpp
YetAnotherFile.cpp
我遇到的问题是不理解外部声明与定义的问题。这个答案有帮助,但您必须阅读整篇文章,然后进行一些研究才能理解它:
定义和声明有什么区别?
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?