Header file causes linker errors (lnk2005) when reused

Header file causes linker errors (lnk2005) when reused

Hi,The attached header file is included in two other header files. Everything is fine if one header file includes it. However, the linker produces errors if two or more header files include it.

Error 4 error LNK2005: "char const * const HK_KEYCODE" (?HK_KEYCODE@@3QBDB) already defined ...Error 5 error LNK2005: "unsigned int const HK_KEYVALUE" (?HK_KEYVALUE@@3IB) already defined ...Error 6 error LNK2005: "public: static class hkTypeInfo const * const * const hkBuiltinTypeRegistry::StaticLinkedTypeInfos" (?StaticLinkedTypeInfos@hkBuiltinTypeRegistry@@2QBQBVhkTypeInfo@@B) already defined ...Error 7 error LNK2005: "public: static class hkClass const * const * const hkBuiltinTypeRegistry::StaticLinkedClasses" (?StaticLinkedClasses@hkBuiltinTypeRegistry@@2QBQBVhkClass@@B) already defined ...Error 8 error LNK2005: "public: static struct hkVersionRegistry::Updater const * * hkVersionRegistry::StaticLinkedUpdaters" (?StaticLinkedUpdaters@hkVersionRegistry@@2PAPBUUpdater@1@A) already defined ...Error 9 error LNK2005: "public: static class hkStaticClassNameRegistry const * * hkVersionRegistry::StaticLinkedClassRegistries" (?StaticLinkedClassRegistries@hkVersionRegistry@@2PAPBVhkStaticClassNameRegistry@@A) already defined ...Error 10 error LNK2005: "char const * const HK_KEYCODE" (?HK_KEYCODE@@3QBDB) already defined ...Error 11 error LNK2005: "unsigned int const HK_KEYVALUE" (?HK_KEYVALUE@@3IB) already defined ...Error 12 error LNK2005: "public: static class hkTypeInfo const * const * const hkBuiltinTypeRegistry::StaticLinkedTypeInfos" (?StaticLinkedTypeInfos@hkBuiltinTypeRegistry@@2QBQBVhkTypeInfo@@B) already defined ...Error 4 error LNK2005: "char const * const HK_KEYCODE" (?HK_KEYCODE@@3QBDB) already defined ...Error 5 error LNK2005: "unsigned int const HK_KEYVALUE" (?HK_KEYVALUE@@3IB) already defined ...Error 6 error LNK2005: "public: static class hkTypeInfo const * const * const hkBuiltinTypeRegistry::StaticLinkedTypeInfos" (?StaticLinkedTypeInfos@hkBuiltinTypeRegistry@@2QBQBVhkTypeInfo@@B) already defined ...Error 7 error LNK2005: "public: static class hkClass const * const * const hkBuiltinTypeRegistry::StaticLinkedClasses" (?StaticLinkedClasses@hkBuiltinTypeRegistry@@2QBQBVhkClass@@B) already defined ...Error 8 error LNK2005: "public: static struct hkVersionRegistry::Updater const * * hkVersionRegistry::StaticLinkedUpdaters" (?StaticLinkedUpdaters@hkVersionRegistry@@2PAPBUUpdater@1@A) already defined ...Error 9 error LNK2005: "public: static class hkStaticClassNameRegistry const * * hkVersionRegistry::StaticLinkedClassRegistries" (?StaticLinkedClassRegistries@hkVersionRegistry@@2PAPBVhkStaticClassNameRegistry@@A) already defined ...Error 10 error LNK2005: "char const * const HK_KEYCODE" (?HK_KEYCODE@@3QBDB) already defined ...Error 11 error LNK2005: "unsigned int const HK_KEYVALUE" (?HK_KEYVALUE@@3IB) already defined ...Error 12 error LNK2005: "public: static class hkTypeInfo const * const * const hkBuiltinTypeRegistry::StaticLinkedTypeInfos" (?StaticLinkedTypeInfos@hkBuiltinTypeRegistry@@2QBQBVhkTypeInfo@@B) already defined ...

Best regards,FY

AllegatoDimensione
Download Havok.h2.6 KB
2 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione

Hey fyisn!
When you build a project, first you compile and then you link. Your errors are link errors, which means all your files have been successfully compiled individually. The linker is trying to pull all the different .o files together in to an executable, but it is finding multiple DEFINITIONS for certain symbols. This is because you have added the contents of havok.h to multiple different files.
When you #include a file, it essentially pastes the contents of the #included file in to the file under compilation. Normally that's not a problem, because our header files just include DECLARATIONS of things (in C and C++ DECLARATION and DEFINITION are two very different things. For example, a function declaration is just the function signature. The definition is the implementation of that function. We can include the function signature, declared in a header, in multiple places, but if we try to include the .c or .cpp file containing the function definition, it will bonk in the manner you have seen.

Specifically, it looks like the LNK2005 errors have to do with KEYCODE and perhaps Registry. On a quick scan through your havok.h file you have included some .cxx files like#include #include Looks suspicious now, doesn't it? That's because .c, .cpp, and .cxx files typically contain definitions of things,not just declarations. That's why you get LNK2005 errors - the two files that include havok.h each have a copy of the DEFINITION of the symbols in the .cxx files.
At the very least you want to remove the .c or .cpp or .cxx files from your header. Generally, headers should include ONLY .h files. Bad stuff happens (like LNK2005) when you #include .c files. Try to clean up your HAVOK include

Hope that helps!

-Josh

Josh Developer Support Engineer Havok www.havok.com

Lascia un commento

Eseguire l'accesso per aggiungere un commento. Non siete membri? Iscriviti oggi