views:

25

answers:

2

This questiod had been brought up numerous times, but Visual Studio never gives up to challange me.

We have an application that should be self sufficient, i.e not depend on any dlls. This is why we build everything statically with MT(d) code generation flags.

The app depends on Qt, zlib, OpenSSL and DCMTK. All of them were built as static libs with MT(d). The app alwo uses some MFC stuff, so we also have to link against it.

MFC is inluded with

#include <afxwin.h>

I read somewhere that this should be the first include in every file, but not sure if it is ture. Anyway, it is not included in every file, just one file inlucdes it.

Here are the link errors:

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

Here is the linker output: http://pastebin.com/9qWbnbC5

I read may threads on many sites as well as the MSDN KB article http://support.microsoft.com/kb/148652, But can't help myself. All of them keep saying that MFC libs should be linked before CRT, but I can not find a way to alter the linking order.

An help is greatly appreciated.

Edit 1: Using the trick from this thread actually resolves the problem, but I still want to know what's wrong here. http://stackoverflow.com/questions/1146338/error-lnk2005-new-and-delete-already-defined-in-libcmtd-libnew-obj Edit 2: Visual Studio 2008 SP1 Windows 7 Qt 4.6.3

A: 

The problem is clear: you are compiling CRT and MFC code together.

When you use the MFC libraries, you must make sure that they are linked before the CRT library is linked. You can do this by making sure that every file in your project includes Msdev\Mfc\Include\Afx.h first, either directly (#include ) or indirectly (#include ). The Afx.h include file forces the correct order of the libraries, by using the directive:

#pragma comment (lib,"<libname>")

Microsoft has an article describing this problem and suggests 2 solutions (step-by-step):

  • Solution One: Force Linker to Link Libraries in Correct Order
  • Solution Two: Locate and Correct the Problem Module
karlphillip
well, how do I located the problem module?
ak
@ak They describe how to do it in the page I pointed above. I suggest you open it and take a closer look.
karlphillip
In that Solution Two they only describe the steps to get the linker output. I already have done that, the linker output is here http://pastebin.com/9qWbnbC5, as I mentioned in the question. But I'm looking at it, and I can only suppose that CRT is linked before MFC, but there is no explanation WHY this is happening...
ak
A: 

This was clarified to me on the MSDN Forumns: http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/4e331cb3-e566-4ca6-b7d4-118c3bebd31a

ak