开发者

Visual Studio Linking errors. Order in which MFC, CRT included?

开发者 https://www.devze.com 2023-01-13 04:02 出处:网络
This question has been brought up numerous times, but Visual Studio never ceases to challenge me. We have an application that should be self-sufficient, i.e. not depend on any 3rd party libraries. Th

This question has been brought up numerous times, but Visual Studio never ceases to challenge me.

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 MT(d) code generation flags.

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

MFC is included via

#include <afxwin.h>

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.

Here are the link-related 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.

I read many threads on many sites as well as this article 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.

Any help is greatly appreciated.

开发者_开发百科Edit 1: Using the trick from this thread actually solves the problem, but I still want to know what's wrong here.

Edit 2: Using Visual Studio 2008 SP1, on Windows 7 and Qt 4.6.3


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 (link now gone, but check here) describing this problem and suggests 2 solutions step-by-step (the following steps are based on Visual C++ 6.0):

Solution One: Force Linker to Link Libraries in Correct Order

  1. On the Project menu, click Settings.
  2. In the Settings For view of the Project Settings dialog box, click to select the project configuration that is getting the link errors.
  3. On the Link tab, click to select Input in the Category combo box.
  4. In the Ignore libraries box, insert the library names (for example, Nafxcwd.lib;Libcmtd.lib).

Note: The linker command-line equivalent in /NOD:<library name>.

  1. In the Object/library modules (VS2008: Properties->Configuration Properties->Linker->Input->Additional Dependencies)box, insert the library names. You must make sure that these are listed in order and as the first two libraries in the line (for example, Nafxcwd.lib Libcmtd.lib).

Solution Two: Locate and Correct the Problem Module

To view the current library link order, follow these steps:

  1. On the Project menu, click Settings.
  2. In the Settings For view of the Project Settings dialog box, click to select the project configuration that is getting the link errors.
  3. On the Link tab, type /verbose:lib in the Project Options box.
  4. Rebuild your project. The libraries will be listed in the output window during the linking process.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号