Search code examples
c++visual-studio-2008lnk

LNK2005 error with vector


This blog Cubic suggested trying four things and then more or less asking for help

  • Rebuild, check
  • check Runtime Libraries, only have one project
  • check entry point, check SUBSYSTEM:CONSULE
  • check for force included .lib files, I do not see #pragma comment(lib, ...)
  • something about turning on /VERBOSE in the linker options, I do not see the option

I would post some code but this is a LNK error ; it does not give much information.

  • LNK2005: "class std::vector,class std::allocator >,class std::allocator,class std::allocator > > >,class std::allocator,class std::allocator >,class std::allocator,class std::allocator > > > > > list1" (?list1@@3V?$vector@V?$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?$allocator@V?$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@@@2@@std@@A) already defined in msproject.obj
  • LNK2005: "class std::vector,class std::allocator >,class std::allocator,class std::allocator > > >,class std::allocator,class std::allocator >,class std::allocator,class std::allocator > > > > > list1" (?list1@@3V?$vector@V?$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?$allocator@V?$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@@@2@@std@@A) already defined in msproject.obj

I see that it is something about vector already defined in msproject - I made sure the vectors have different names. Is this something with the header # include <vector>; I tried commenting out some of the #includes to check, but same.


Solution

  • Seems like you're defining list1 more than once. (btw, the name suggests it should be a std::list, but that's beyond the scope of the question)

    Do you have a

    std::vector<std::string> list1;
    

    in a header file? Is that header included in multiple translation units?

    If you want a global, you need to use extern in the header:

    extern std::vector<std::string> list1;
    

    and move the definition in a single implementation file.