Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LNK2005 (already defined)

Tags:

c++

lnk2005

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.

like image 211
takecare Avatar asked Nov 17 '11 16:11

takecare


1 Answers

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.

like image 154
John Dibling Avatar answered Sep 26 '22 20:09

John Dibling