title: Visual Studio Linking errors. Order in which MFC, CRT included?
body: <p>This question has been brought up numerous times, but Visual Studio never ceases to challenge me.</p>
  
  <p>We have an application that should be self-sufficient, i.e. not depend on any 3rd party libraries. This is why we build everything statically using the <code>MT(d)</code> code generation flags.</p>
  
  <p>The app depends on <code>Qt</code>, <code>zlib</code>, <code>OpenSSL</code> and <code>DCMTK</code>. All of these libraries were built as static libs with <code>MT(d)</code>. The app also uses some <code>MFC</code>-related code, so we also have to link against it.</p>
  
  <p>MFC is included via</p>
  
  <pre><code>#include &lt;afxwin.h&gt;
  </code></pre>
  
  <p>I read somewhere that this should be the first include in every file, but I'm not sure if it is true. Anyway, the line is not included in every file, only one file includes it.</p>
  
  <p>Here are the link-related errors:</p>
  
  <pre><code>Error 24 error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj) uafxcwd.lib
  Error 22 error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj) uafxcwd.lib
  Error 23 error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmtd.lib(newaop.obj) uafxcwd.lib
  Error 21 error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj) uafxcwd.lib
  </code></pre>
  
  <p>Here is the <a href="http://pastebin.com/9qWbnbC5" rel="nofollow noreferrer">linker output</a>.</p>
  
  <p>I read many threads on many sites as well as this <a href="http://support.microsoft.com/kb/148652" rel="nofollow noreferrer">article</a> from MSDN's KB. But they don't help me, as all of them keep saying that MFC libs should be linked before CRT, but I cannot find a way to alter the linking order.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p><strong>Edit 1</strong>:
  Using the trick from <a href="https://stackoverflow.com/questions/1146338/error-lnk2005-new-and-delete-already-defined-in-libcmtd-libnew-obj">this thread</a> actually solves the problem, but I still want to know what's wrong here.</p>
  
  <p><strong>Edit 2</strong>: 
  Using Visual Studio 2008 SP1, on Windows 7 and Qt 4.6.3</p>