A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
来源:互联网 发布:c语言小写字母转大写 编辑:程序博客网 时间:2024/05/20 07:15
SYMPTOMS
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)
"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)
"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)
"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)
MSVCRTD.LIB (dllmain.obj)
mfcs42d.lib(dllmodul.obj): error LNK2005: _DllMain@12 already defined in
msvcrtd.lib(dllmain.obj)
msvcrtd.lib(dllmain.obj)
CAUSE
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.
RESOLUTION
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.
Note The following steps are based on Visual C++ 6.0.
Solution One: Force Linker to Link Libraries in Correct Order
- On the Project menu, click Settings.
- In the Settings For view of the Project Settings dialog box, click to select the project configuration that is getting the link errors.
- On the Link tab, click to select Input in the Category combo box.
- 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>. - 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:- On the Project menu, click Settings.
- In the Settings For view of the Project Settings dialog box, click to select the project configuration that is getting the link errors.
- On the Link tab, type /verbose:lib in the Project Options box.
- Rebuild your project. The libraries will be listed in the output window during the linking process.
STATUS
This behavior is by design.
MORE INFORMATION
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 <Afx.h>) or indirectly (#include <Stdafx.h>). The Afx.h include file forces the correct order of the libraries, by using the #pragma comment (lib,"<libname>") 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:
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:
- Open Msdev/Mfc/Include/Afx.h.
- Select the lines between #ifndef _AFX_NOFORCE_LIBS and #endif //!_AFX_NOFORCE_LIBS.
- Copy the selection to the Windows Clipboard.
- Create a new text file.
- Paste the contents of the Clipboard into this new file.
- Save the file as Msdev/Mfc/Include/Forcelib.h.
Steps to Reproduce the Problem in Visual C++ .NET
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- Click Visual C++ Projects under Project Types, and then click MFC Applicationunder Templates.
- In the Name text box, type Q148652.
- In the Location text box, type C:/Test, and then click OK.
- In the MFC Application Wizard dialog box, click Application Type.
- Click Dialog based under Application type, and then click Use MFC in a static library under Use of MFC.
- Click Finish.
- In Solution Explorer, under Source Files select all the three .cpp files.
- Right-click the three selected files, and then click Remove.
- Right-click Source files, point to Add, and then click Add New Item.
- Click C++ files under Templates. In the Name text box, type Aa. Click Open.
- Paste the following code in the Aa.cpp file:
int test(){new int; return 1;}
- Right-click Source Files, point to Add, and then click Add Existing Item.
- Select the following files:
- Q148652.cpp
- Q148652Dlg.cpp
- stdafx.cpp
- Click Open.
- The files that you selected in step 15 appear under Source Files.
- Select all four .cpp files under Source Files.
- Right-click the four .cpp files that you selected, and then click Properties.
- Expand Configuration Properties, and then expand C/C++.
- Click Precompiled Headers.
- Set the Create/Use Precompiled Header property to Not Using Precompiled Headers. Click OK.
- On the Build menu, click Rebuild Solution.
A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
(原文URL:http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q148652)
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visua
- zz : A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++ ---- nafxcw.lib(a
- How to solve problem caused when the CRT library and MFC libraries are linked in the wrong order
- When the C Run-Time (CRT) library and Microsoft Foundation Class (MFC) libraries are linked in the w
- Typically this problem occurs when you are putting in a data that is too long for the column. In t
- What are the differences between a pointer variable and a reference variable in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- You receive a C4226 or a C4236 error message when you compile code to contain the keyword in 32-bit Visual C++(ZZ)
- how to find the corresponding libraries are linked to when some APIs are used(2.0)
- how to find the corresponding libraries are linked to when some APIs are used(3.0)
- What are the differences between struct and class in C++?
- Why are the letters in the English "alphabet" in the order that they are?
- “Use MFC in a Static Library” -> CRT static
- JBoss Cache NonManagedConnectionFactory will log the password in clear text when an exception occurs
- Question 6: Which of the following are container adapters in the STL (Standard Template Library) in C++?
- Use MFC in a Static Library,This may be due to a corruption of the heap....
- 数据结构前言之想
- 【转】IEEE802.1协议族面向的领域(转自wikipedia)
- 利用 Int2Type 进行函数类型切换
- 毕业了
- Apache Commons Validator
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
- tld 文件中的body-context的枚举
- libvirtd远程访问被拒绝原因之一
- commonDialog引发AccessViolationException的一种原因
- 日本语学习网站汇总
- JAVA反射机制
- Linux与FreeBSD系统的十个本质区别
- C++中时间处理
- Java 程序员学习 C#笔记 (2)