How to get a multiply-defined symbol
There are many reasons why the "multiply-defined" error can occur at link-time, most of them annoying but mundane: failure to include compilation guards on header files, the compiler finding more than one version of a file (perhaps a backup?) on the file-search path, lack of the extern qualifier and so on. (See below.) But what if it's none of these things? All the headers have the right compilation guards on them, you've searched for duplicate files and grepped for namespace collisions. Here's a possible cause that kept me guessing for three days, until I identified it:
// file manipaction.h #pragma once #ifndef MANIPACTION_H #define MANIPACTION_H // *** INCLUDES #include "Action.h" #include "ActionUtils.h" #include "SblDebug.h" #include "EvolRule.cpp" #include "MesaTree.h" #include "CharComparator.h" #include "MesaGlobals.h" ...
See the problem? A typo has resulted in the source of a class being included instead of the header. This happens quietly and the problem only start when other files include the header containing the mangled #include statement. Also, the linker will probably not identify the correct header as where the problem is. However, these errors can be tracked down by grepping for .cpp or #include "[^.]*.cpp in headers:
// *** INCLUDES #include "Action.h" #include "ActionUtils.h" #include "SblDebug.h" #include "EvolRule.h" #include "MesaTree.h" #include "CharComparator.h" #include "MesaGlobals.h" ...
References
- Thanks to Kirk Swenson whose posting put me on the right track to sorting this out.
- Google for many other examples of how to get this error.