title: Getting error LNK2005 in Visual Studio 2013 on std::string methods and operators
body: <p>I have a Qt-based project that I have been building and using Visual Studio 2008 for about 5 years.  I build and use both 'Debug' and 'Release' build configurations.</p>
  
  <p>I'm migrating to VS2013 and now I'm getting some rather strange LNK2005 errors on std::string methods and operators.</p>
  
  <p>The code base is broken into two libraries and a bunch of UI directories.  In Debug build configuration the two libraries are built into DLLs and then must be specified in the PATH when running one of the UIs.  In Release mode everything is compiled statically into a single executable.  Works great.</p>
  
  <p>The two libraries are 'model' and 'engine'.  The 'model' is built first and the 'engine' is then built.  The 'engine' uses the 'model' (library).</p>
  
  <p>When the 'engine' is linked, I get a bunch of the following errors, all dealing with std::string:</p>
  
  <pre><code>    1&gt;model.lib(model.dll) : error LNK2005: "public: class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; 
          &amp; __thiscall std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;::operator=(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;)" 
          (??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@ABV01@@Z) 
          already defined in NamedObject.obj
  
      1&gt;model.lib(model.dll) : error LNK2005: "public:
          int __thiscall std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;::compare(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const &amp;)const " 
          (?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEHABV12@@Z) 
          already defined in NamedObject.obj
  </code></pre>
  
  <p>The first looks like the 'std::string::operator=(const std::string &amp;)' operator.  The 'NamedObect' class is in 'engine' and does NOT override 'std::string::operator=()'.  In fact, 'NamedObject' is basically a simple wrapper around a 'std::string' attribute, an accessor and a 'bool operator== (const NamedObject &amp;)' method, that looks like the following:</p>
  
  <pre><code>    bool
      NamedObject::operator== (const NamedObject &amp;v)
      { return (name == v.name); }
  </code></pre>
  
  <p>There are many other LNK2005 errors like this, but this one is fairly isolated to a small class.</p>
  
  <p>Things I've checked/verified:</p>
  
  <ul>
  <li>I'm compiling and linking with consistent '/Mxx' compiler options (/MDd) in this case.</li>
  <li>The '-Zc:wchar_t' option is specified in both the Makefiles for the 'model' and 'engine'.</li>
  <li>Both libraries link with '/NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /NODEFAULTLIB:LIBCMT /NODEFAULTLIB:LIBCPMT /NODEFAULTLIB:LIBCMTD /NODEFAULTLIB:LIBCPMTD /NODEFAULTLIB:MSVCRT /NODEFAULTLIB:MSVCPRT /DLL'</li>
  <li>A 'dumpbin /dependencies' on the various object files reveal '/DEFAULTLIB:msvcprtd /DEFAULTLIB:MSVCRTD /DEFAULTLIB:OLDNAMES'.</li>
  </ul>
  
  <p>I. Am. Stuck.</p>
  
  <p><strong>EDIT</strong>. More details. I did a dumpbin /all model.dll on both versions (VS2008 and VS2013) and found that, indeed, the VS2013 version included std::string implementations: <code>??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@PBD@Z (public: class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; &amp; __thiscall std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;::operator=(char const *))</code> </p>
  
  <p>but the VS2008 version did not.</p>
  
  <p><strong>EDIT 2</strong> Well, I decided to give this the 'ol college try.....  I've isolated this to just a few small classes (and yes, everything is compiled with the same version of the compiler, same VS solution, etc., etc....).</p>
  
  <p>Here's what I have for the declaration of a simple 'model' class.  I'll explain later about some stuff.  You can sort of figger out what the implementation of the methods are.....</p>
  
  <pre><code># pragma once 
  # if defined(WIN32) &amp;&amp; defined(BUILD_DLL)
  #     if defined(MODEL_EXPORT_API) // inside DLL
  #         define MODEL_EXPORTAPI __declspec(dllexport)
  #         define MODEL_EXPIMP_TEMPLATE
  #     else // outside DLL
  #         define MODEL_EXPORTAPI __declspec(dllimport)
  #         define MODEL_EXPIMP_TEMPLATE extern
  #     endif
  # else
  #     define MODEL_EXPORTAPI
  # endif
  
  # include &lt;string&gt;
  # if defined (WIN32)
      MODEL_EXPIMP_TEMPLATE template class MODEL_EXPORTAPI std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;;
  # endif
  
  namespace model { namespace exceptions {
      class MODEL_EXPORTAPI FlowException {
      public:
          FlowException(const std::string &amp; m);
          virtual ~FlowException();
          virtual const char *  what() const;
      private:
           // Copy constructor and assignment operator are private and
          // unimplemented.
          FlowException(const FlowException &amp;);
          FlowException &amp;    operator=(const FlowException &amp;);
  
          std::string _msg;
     };
  } }
  </code></pre>
  
  <p>For 'engine' classes.  This one is similar to the 'model' class above.  Again, you can figger out what the implementation would be...</p>
  
  <pre><code># pragma once
  
  # include &lt;string&gt;
  
  # if    defined(WIN32) &amp;&amp; defined(BUILD_DLL)
  #    if defined(FLOWENGINE_EXPORT_API) // inside DLL
  #       define FLOWENGINE_EXPORTAPI   __declspec(dllexport)
  #       define FLOWENGINE_EXPIMP_TEMPLATE
  #    else // outside DLL
  #       define FLOWENGINE_EXPORTAPI   __declspec(dllimport)
  #       define FLOWENGINE_EXPIMP_TEMPLATE extern
  #    endif
  # else
  #    define FLOWENGINE_EXPORTAPI
  # endif
  
  namespace flowEngine { namespace core {
  
  class NamedObject {
  public:
      NamedObject(const std::string &amp; n);
  
      virtual ~NamedObject();
  
      bool        operator== (const NamedObject &amp; v) const;
  
      const char *    getName() const;
  
  private:
      // Copy constructor and assignment operator are private and
      // unimplemented.
      NamedObject(const NamedObject &amp;);
      NamedObject &amp;   operator=(const NamedObject &amp;);
  
      std::string     _name;
  };
  } }
  </code></pre>
  
  <p>Now for the tests. </p>
  
  <ol>
  <li><p>If I don't have that 'MODEL_EXPIMP_TEMPLATE template class...' line in the 'model exception' class, then I get 'warning C4251: 'model::exceptions::FlowException::_msg' : class 'std::basic_string,std::allocator>' needs to have dll-interface to be used by clients of class 'model::exceptions::FlowException'' on each reference of that header file and this example links.</p>
  
  <ol start="2">
  <li><p>If I 'include' that 'exception' class in the model into a class in the engine, and w/o that 'MODEL_EXPIMP_TEMPLATE template class...' line this example works (both libraries link).</p></li>
  <li><p>If I include that 'exception' class in the model into a class in the engine WITH THE ''MODEL_EXPIMP_TEMPLATE template class...' line in the exception class, then this example fails when the engine library is linked with: '1>model.lib(model.dll) : error LNK2005: "public: __thiscall std::basic_string,class std::allocator >::basic_string,class std::allocator >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in NamedObject.obj'</p></li>
  </ol></li>
  </ol>
  
  <p>Now what?  In one case the compiler is telling to fix the code and when I do links fail....</p>