A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++

当CRT库和MFC库链接顺序错误时,可能会遇到LNK2005错误。此类错误通常涉及重复定义的新、删除和DllMain函数。解决方法包括强制链接器以正确的顺序链接库文件或定位并修正导致问题的模块。

 

 


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)
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)
 

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.
 

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

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.
 

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:
  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.
 

Steps to Reproduce the Problem in Visual C++ .NET

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C++ Projects under Project Types, and then click MFC Applicationunder Templates.
  4. In the Name text box, type Q148652.
  5. In the Location text box, type C:/Test, and then click OK.
  6. In the MFC Application Wizard dialog box, click Application Type.
  7. Click Dialog based under Application type, and then click Use MFC in a static library under Use of MFC.
  8. Click Finish.
  9. In Solution Explorer, under Source Files select all the three .cpp files.
  10. Right-click the three selected files, and then click Remove.
  11. Right-click Source files, point to Add, and then click Add New Item.
  12. Click C++ files under Templates. In the Name text box, type Aa. Click Open.
  13. Paste the following code in the Aa.cpp file:
    int test(){new int; return 1;}
  14. Right-click Source Files, point to Add, and then click Add Existing Item.
  15. Select the following files:
    • Q148652.cpp
    • Q148652Dlg.cpp
    • stdafx.cpp
  16. Click Open.
  17. The files that you selected in step 15 appear under Source Files.
  18. Select all four .cpp files under Source Files.
  19. Right-click the four .cpp files that you selected, and then click Properties.
  20. Expand Configuration Properties, and then expand C/C++.
  21. Click Precompiled Headers.
  22. Set the Create/Use Precompiled Header property to Not Using Precompiled Headers. Click OK.
  23. 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)

 

### 三级标题:解决 error LNK2005: _DllMain@12 already defined in StudentInfoDLL.obj 当在构建 DLL 项目时,链接器报错 `error LNK2005: _DllMain@12 already defined`,通常表示 `DllMain` 函数被多次定义。此问题通常出现在 MFC DLL 项目中,当开发者手动定义了 `DllMain` 函数,而 MFC 的库文件(如 `mfcsXXd.lib`)也提供了默认实现时,导致链接冲突[^1]。 #### 使用 `#pragma comment(linker, "/include:__afxForceUSRDLL")` 强制链接器处理 MFC DLL 导出符号 MFC 提供了一种机制,通过在源文件中添加如下预处理指令,可以强制链接器包含必要的符号,从而避免 `DllMain` 的重复定义: ```cpp #ifdef _USRDLL #pragma comment(linker, "/include:__afxForceUSRDLL") #endif ``` 此代码应放置在 DLL 的主实现文件(如 `StudentInfoDLL.cpp`)中,确保链接器正确处理 MFC 的 DLL 导出符号[^4]。 #### 避免在 DLL 中手动定义 `DllMain` MFC DLL 项目默认由 MFC 提供 `DllMain` 实现,若开发者手动添加 `DllMain` 函数,则会导致重复定义错误。应删除手动定义的 `DllMain` 函数,并依赖 MFC 提供的默认实现。如果需要在 DLL 初始化或卸载时执行自定义逻辑,可以通过重写 `CWinApp::InitInstance` 和 `CWinApp::ExitInstance` 方法实现[^3]。 #### 检查 MFC 链接方式与项目配置一致性 确保项目属性中“使用 MFC”选项与链接的 MFC 库版本一致。例如,若项目使用的是“在共享 DLL 中使用 MFC”,则链接的 MFC 库应为 `mfcsXXd.dll`(调试模式)或 `mfcsXX.dll`(发布模式)。不一致的配置会导致链接器错误,包括 `DllMain` 符号冲突[^1]。 #### 避免在扩展 DLL 中使用 `AFX_MANAGE_STATE(AfxGetStaticModuleState())` 在某些 MFC 扩展 DLL 场景中,若错误地使用了 `AFX_MANAGE_STATE(AfxGetStaticModuleState())`,可能会导致 `DllMain` 冲突。此宏用于管理模块状态,但在扩展 DLL 中不应使用,因为其内部逻辑与 MFC 默认的 DLL 初始化机制冲突[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值