用VC写DLL中"error LNK2005: _DllMain@12 already defined"的应对

部署运行你感兴趣的模型镜像

用Visual C++编写DLL,如果在new project时选了MFC DLL,而后又想写成Regular DLL,即拥有自己的DllMain()入口函数,则在build时会遇到类似如下的link错误:

 error LNK2005: _DllMain@12 already defined in xxx.OBJ

几种解决方案:

1, 你只需要在工程设置里面,把

WIN32,NDEBUG,_WINDOWS,_MBCS,_USRDLL,MSGBOX_EXPORTS,_WINDLL,_AFXDLL

中的_USRDLL,删除,就可以正确编译了

2,http://support.microsoft.com/default.aspx?scid=kb;en-us;q148652

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

3, http://blog.edu.cn/user3/RFox/archives/2008/2140436.shtml

I want to add some initial code in DllMain in a MFC dll project, but after I added the code and compiled, there was a link error:

mfcs42d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in DLLMAIN.OBJ

DLLMAIN.cpp is the file I created by my own and I define DllMain() in it.

What’s the reason? the linker complains that I have a DllMain in DLLMAIN.cpp but there’s another DllMain in mfcs42d.lib.

So how to use my own DllMain if a MFC dll project?  There’s a quick answer on codeguru , but that article just show the tip without explaining it with more details.

The article says, just copy MFC’s dllmodule.cpp into your own project and compile, it will be OK. It seems to be nonsense but after I tried I found it works. But why? By commenting out unnecessary lines, I find these lines are the key point: //(dllmodule.cpp)

// The following symbol used to force inclusion of this module for _USRDLL #ifdef _X86_ extern “C” { int _afxForceUSRDLL; } #else extern “C” { int __afxForceUSRDLL; } #endif

Do you noticed the comment? it forces the inclusion of the module of dllmodule.obj. But how? A searching for _afxForceUSRDLL in MFC source code gives me the answer: //afx.h

// force inclusion of DLLMODUL.OBJ for _USRDLL #ifdef _USRDLL #pragma comment(linker, “/include:__afxForceUSRDLL”) #endif

Again the MFC designer gives us a good comment: “force inclusion of DLLMODUL.OBJ”, OK, got it.

Now let summary it up: 1)When you try to use MFC library, you surely will include afx.h directly or indirectly 2)then MFC(afx.h) tell the linker to find the symbol of __afxForceUSRDLL and put that object which contains __afxForceUSRDLL into the program, so linker searches and puts dllmodule.obj into my program, for __afxForceUSRDLL is defined in dllmodule.cpp That’s the common scenario.

Then you want to use your own DllMain in a mfc dll project, linker complains that there are two DllMain, one in your code, one in Dllmodule.obj.

The solution? Tell the linker to add my dllmain.obj for __afxForceUSRDLL. So we define __afxForceUSRDLL in our own cpp file where our own DllMain is defined, then the linker will ignore mfc’s dllmodule.obj and see only one DllMain and never complains.

So the solution is just to add extern “C” { int _afxForceUSRDLL; } in the file where your own DllMain is defined, copying mfc’s dllmodule.cpp is not necessary :-)

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

### 三级标题:解决 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`)中,确保链接器正确处理 MFCDLL 导出符号[^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]。 ---
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值