Question

I currently have two DLL libraries - CMN and GT. GT is dependent on CMN.

In Visual Studio 2003, I can compile and link both libraries without issue. I can compile CMN and GT successfully in Visual Studio 2010, and link CMN. If I try to link GT however, I get the following errors:

CMN.lib(CMN.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in TokenizerAdvanced.obj
CMN.lib(CMN.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in TokenizerAdvanced.obj
CMN.lib(CMN.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in TokenizerAdvanced.obj
CMN.lib(CMN.dll) : error LNK2005: "public: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::reserve(unsigned int)" (?reserve@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEXI@Z) already defined in TokenizerAdvanced.obj
CMN.lib(CMN.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::erase(unsigned int,unsigned int)" (?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@II@Z) already defined in TokenizerAdvanced.obj

TokenizerAdvanced is a source file in GT.

All compiling and linking is done with the same command-line structure (using -MDd to compile). What has changed between Visual Studio 2003 and 2010 that would cause this issue?

Edit: Strangely enough, if I exclude TokenizerAdvanced.cpp from the build (the other file - Tokenizer.cpp does not reference it), I get an unresolved external error:

Tokenizer.obj : error LNK2001: unresolved external symbol "public: static unsigned int const std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::npos" (?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2IB)

Edit: Moved investigation into answer.

Was it helpful?

Solution

I think I have found the problem. We have classes that derive from string in CMN. See here.

This discussion suggests adding the following to the client library source code:

template std::string::size_type std::string::npos;
template std::wstring::size_type std::wstring::npos;

But this didn't solve the problem for me.

This discussion suggests changing any classes that derive from string (and I have noticed that our code does so in a few places) to use containment instead, which is what I ended up doing. This fixed the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top