简体   繁体   中英

Visual Studio 2010 C++ awkward linker erratic behaviour

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM