编译DLL时"error LNK2005: _DllMain@12 already defined"错误

在编译DLL时遇到错误'error LNK2005: _DllMain@12 already defined'。该问题可能由于CRT库和MFC库链接顺序不正确导致。解决方案包括删除预处理器定义中的_USRDLL或调整链接器的库链接顺序。通过在Visual C++项目设置中查看并调整链接顺序,可以解决此问题。

编译出现如下错误


nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in QQInfo.obj
nafxcwd.lib(dllmodul.obj) : warning LNK4006: _DllMain@12 already defined in QQInfo.obj; second definition ignored
   Creating library Debug/QQInfo.lib and object Debug/QQInfo.exp
Debug/QQInfo.dll : fatal error LNK1169: one or more multiply defined symbols found


解决方法:

project setting->C/C++, 在category中选择Preprocessor,将preprocessor definitions中的_USRDLL 删除 即可。

 


网上查找过程中,MSDN也有一些应对资料


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

 

几种解决方案内容如下:

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

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.

 

 

 

### 三级标题:解决 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]。 ---
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值