LNK2005 (already defined)

takecare picture takecare · Nov 17, 2011 · Viewed 16k times · Source

I know you've seen 21728517 people asking for help with this one but after searching and reading about this I really can't figure this one out. I know this error, I've seen it before, however, this time, I can't seem to get around it.

I've also tried this checklist.

So, the errors:

Error   25  error LNK2005: "void __cdecl checkStatus(unsigned int &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool)" (?checkStatus@@YAXAAIV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z) already defined in DollarRecognizer.obj C:\Users\Rui Teixeira\Desktop\Current\Tese\SVN\TIFEE_Empty\TIFEE_Empty\main.obj TIFEE_Empty

Error   26  error LNK2005: "void __cdecl depth2rgb(unsigned short const *,unsigned short *,char *,int,int)" (?depth2rgb@@YAXPBGPAGPADHH@Z) already defined in DollarRecognizer.obj  C:\Users\Rui Teixeira\Desktop\Current\Tese\SVN\TIFEE_Empty\TIFEE_Empty\main.obj TIFEE_Empty

Error   27  error LNK2005: "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __cdecl explode(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char)" (?explode@@YA?AV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@D@Z) already defined in DollarRecognizer.obj    C:\Users\Rui Teixeira\Desktop\Current\Tese\SVN\TIFEE_Empty\TIFEE_Empty\main.obj TIFEE_Empty

So, the thing is, these are functions defined in "misc.h" with the proper #ifndef #define #endif. They are not defined elsewhere but I still get the LNK2005 of death. What am I doing wrong?

Thanks in advance.

Answer

John Dibling picture John Dibling · Nov 17, 2011

Define the functions in misc.cpp, rather than in misc.h.

The problem is probably due to #includeing misc.h in multiple CPP files. the header guards prevent a header from being included multiple times in the same translation unit, but each CPP file is (typically) a separate translation unit. So those functions end up getting defined twice -- once in each translation unit.