Tuesday, December 02, 2008

MFC and Standard CRT Runtime Conflict

When linking an exe with many dependencies one useful thing to do to make sure the links paths are correct is to
add /verbose:lib under the Project Property Page under Linker in the Command Line Additionnal Options text field (Visual Studio 2008). Doing this will output the full path of each library loaded by the linker when the linker will build your final binary (EXE, DLL for instance).

It is also important to make sure not to mix MFC and non MFC runtime. Many time even if you are not using MFC you can generate a static library using the MFC runtime in order to avoid those anoying nodefault library line.

A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
View products that this article applies to.
This article was previously published under Q148652
On This Page

* SYMPTOMS
* CAUSE
* RESOLUTION
o Solution One: Force Linker to Link Libraries in Correct Order
o Solution Two: Locate and Correct the Problem Module
* STATUS
* MORE INFORMATION
o Steps to Reproduce the Problem in Visual C++ .NET

Expand all | Collapse all
SYMPTOMS
When the C Run-Time (CRT) library and Microsoft Foundation Class (MFC) librarie...
When the C Run-Time (CRT) library and Microsoft Foundation Class (MFC) libraries are linked in the wrong order, you may receive one of the following LNK2005 errors:
nafxcwd.lib(afxmem.obj) : error LNK2005:
"void * __cdecl operator new(unsigned int)"(??2@YAPAXI@Z) already
defined in LIBCMTD.lib(new.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005:
"void __cdecl operator delete(void *)"(??3@YAXPAX@Z) already defined
in LIBCMTD.lib(dbgnew.obj)
nafxcwd.lib(afxmem.obj) : error LNK2005:
"void * __cdecl operator new(unsigned int,int,char const *,int)"
(??2@YAPAXIHPBDH@Z) already defined in LIBCMTD.lib(dbgnew.obj)
mfcs40d.lib(dllmodul.obj): error LNK2005: _DllMain@12 already defined in
MSVCRTD.LIB (dllmain.obj)
mfcs42d.lib(dllmodul.obj): error LNK2005: _DllMain@12 already defined in
msvcrtd.lib(dllmain.obj)
Back to the top
CAUSE
The CRT libraries use weak external linkage for the new, delete, and DllMain fu...
The CRT libraries use weak external linkage for the new, delete, and DllMain functions. The MFC libraries also contain new, delete, and DllMain functions. These functions require the MFC libraries to be linked before the CRT library is linked.

Back to the top
RESOLUTION
There are two ways to resolve this problem. The first solution involves forcing...
There are two ways to resolve this problem. The first solution involves forcing the linker to link the libraries in the correct order. The second solution allows you to find the module that is causing the problem and to correct it.

Note The following steps are based on Visual C++ 6.0.
Back to the top
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:.
5. In the Object/library modules 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).

To set this option in Visual C++ .NET, read the "Setting Visual C++ Project Properties" online help topic.
Back to the top
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.

Back to the top
STATUS
This behavior is by design.
This behavior is by design.
Back to the top
MORE INFORMATION
When you use the MFC libraries, you must make sure that they are linked before...
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 #pragma comment (lib,"") directive.

If the source file has a .c extension, or the file has a .cpp extension but does not use MFC, you can create and include a small header file (Forcelib.h) at the top of the module. This new header makes sure that thelibrary search order is correct.

Visual C++ does not contain this header file. To create this file, follow these steps:

1. Open Msdev\Mfc\Include\Afx.h.
2. Select the lines between #ifndef _AFX_NOFORCE_LIBS and #endif //!_AFX_NOFORCE_LIBS.
3. Copy the selection to the Windows Clipboard.
4. Create a new text file.
5. Paste the contents of the Clipboard into this new file.
6. Save the file as Msdev\Mfc\Include\Forcelib.h.


APPLIES TO

* Microsoft Visual C++ 2008 Express Edition
* Microsoft Visual C++ 4.0 Standard Edition
* Microsoft Visual C++ 4.1 Subscription
* Microsoft Visual C++ 5.0 Enterprise Edition
* Microsoft Visual C++ 6.0 Enterprise Edition
* Microsoft Visual C++ 5.0 Professional Edition
* Microsoft Visual C++ 6.0 Professional Edition
* Microsoft Visual C++, 32-bit Learning Edition 6.0
* Microsoft Visual C++ .NET 2002 Standard Edition
* Microsoft Visual C++ .NET 2003 Standard Edition

http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q148652

No comments: