Visual Studio 2010 C++尴尬的链接器不稳定的行为
在从事一个大项目时,我遇到了这个问题:链接器有时会拒绝“查看”类的实现,在找到该特定类(板)的方法时抛出“符号未找到”异常。
一些“技术”细节:
我在 .cpp 文件上使用了 #pragma 一次。我知道最终他们不会做(或不应该做)任何事情,因为 .cpp 文件不包含在其他文件中,但我养成了在我创建的每个代码文件的顶部编写该文件的坏习惯。
该类实现其 .h 文件中声明的所有方法
该类包含项目的其他标头,但相同的标头 存在编译问题的类。
在我编写了我的类包含并使用的模板类后不久,问题就开始出现,但是,如上所述,其他类没有问题(所以我无法判断模板是否真正相关)问题)。
最奇怪的部分是:
- 当链接器因“未找到符号”异常而失败时(它仅在某些时候执行此操作),如果我在类 .cpp 文件中切换 #pragma Once 指令,则下一次编译将起作用。这意味着有时我需要注释“#pragma Once”才能成功编译,有时我需要取消注释它。
如果您需要更多信息,例如该类的 .h 和 .cpp 文件,我会尽快写。
感谢您抽出时间!
编辑:
更多信息:
当问题第一次出现时,我尝试注释尽可能多的类方法和字段,试图隔离问题。有时,删除类和模板之间的链接会导致良好的编译,但后来这并不重要。在我的项目的不同部分(文件/命名空间/等)添加类的实例也没有任何区别。
Visual Studio 安装在 C:\,项目位于 D:\,两个分区都是 NTFS。
这是我唯一遇到问题的项目,在问题开始之前和之后,我已经使用相同的编程风格成功地制作、编译和运行了许多其他项目。
当问题出现时,我尝试重建整个项目并手动清理.obj文件,但没有任何区别。
Working on a big project I have bumped into this problem: from time to time the linker refuses to "see" the implementation of a class, throwing "symbol not found" exceptions wherever methods of that particular class (Board) are found.
Some 'technical' details:
I use #pragma once on my .cpp files. I know that in the end they don't do (or shouldn't do) anything since .cpp files are not included in others, but I developed this bad habit of writing that at the top of every code file I create.
The class implements ALL of the methods declared in its .h file
The class includes other headers of the project, but the same headers are included in many files, yet this particular class is the ONLY one which has compilation problems.
The problems began to appear shortly after I wrote a template class which my class includes and uses, but, as mentioned above, other classes have no problem with it (so I can't tell whether the template is or not truly related to the problem).
and the weirdest part is:
- When the linker fails with the "symbol not found" exception (it does this only SOME of the times), if I toggle the #pragma once directive in the class .cpp file, the next compilation works. This means that sometime I need to comment "#pragma once" in order to get a successful compilation, other times I need to uncomment it.
If you need any more information, like the .h and .cpp files of the class, I will write it asap.
Thanks for your time!
EDIT:
Some more info:
When the problem first appeared, I tried to comment as many methods and fields of the class as possible, trying to isolate the problem. On an occasion, removing the link between the class and the template resulted in a good compile, but later on it didn't really matter. Adding instances of the class in different parts of my project (files/namespaces/etc) didn't make any difference either.
Visual Studio is installed on C:\, the project is on D:\, both partitions are NTFS.
This is the only project I have problems with, I have made and compiled and ran successfully many other projects before and after the problem began on this one with the same programming style.
I tried rebuilding the whole project and cleaning up .obj files by hand when the problem appears and it did not make any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此链接http://www.cplusplus.com/forum/beginner/7877/为不同风格的包含守卫提供有趣的讨论。
这表明 #pragma 一旦失败的情况可能会对您有所帮助。
我的感觉是,对于可移植代码,避免 #pragma 一次。
This link http://www.cplusplus.com/forum/beginner/7877/ provides interesting discussion for the different styles of incude guards.
That suggests cases when #pragma once fails , may be that can help you.
My feeing is to avoid #pragma once, for portable code.