"error LNK2005: _DllMain@12 already defined"的解决办法

error:LNK2005 已经在*.obj中定义 的原因分析及对策 /如果没有定义这个宏//定义这个宏//...code...头文件主体内容endif我经常忽略的一个细节,举例说明:class Cubepublic:Cube();~Cube();m_a = a;m_a = b;m_a = c;int getV();//声明了getV().想要在外部单独定义private:int m_a;int m_b;int m_c;int m_v;return m_v;变量在使用前就要被定义或声明; 阅读详情

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

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

 

这是因为_USRDLL和_AFXDLL发生冲突所致。

 

几种解决方案:

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

解决LNK2005错误的方法和示例代码 在你的问题中,你提到了一个具体的符号:msvcprtd.lib。为了解决这个问题,你可以检查头文件的包含方式、全局变量的定义和声明、编译器选项的设置,以及使用命名空间等。头文件被错误地包含多次:如果你在多个源文件中包含了相同的头文件,可能会导致重复定义的符号。定义了重复的全局变量:如果你在多个源文件中定义了相同名称的全局变量,也会导致LNK2005错误。使用命名空间来避免符号冲突:如果你在不同的源文件中使用了相同名称的函数或类,可以尝试将它们放置在不同的命名空间中,以避免符号冲突。 阅读详情

相关推荐

error LNK2005: 找到一个或多个多重定义的符号已经在xxxx.obj中定义的解决方法

总结一下,当我们在进行C/C++编程时,如果出现error LNK2005错误提示"找到一个或多个多重定义的符号已经在xxxx.obj中定义"时,通常表示在多个源文件中定义了相同的全局变量、函数或类等。为了解决这个问题,我们可以使用extern关键字来声明全局变量、函数或类的原型,并将其定义放在单独的源文件中。除了全局变量外,我们也可能在多个源文件中定义了相同的函数或类。在上述示例中,我们在main.cpp中声明了printMessage()函数,并在utils.cpp中进行了定义。

PixelShadeZ的博客 2416

error:LNK2005解决

造成LNK2005错误主要有以下几种情况: 1.重复定义全局变量。可能存在两种情况: A、对于一些初学编程的程序员,有时候会以为需要使用全局变量的地方就可以使用定义申明一下。其实这是错误的,全局变量是针对整个工程的。   切记的就是不能给变量赋值否则还是会有LNK2005错误。 (1)声明必须使用extern关键字 (2)**不能给变量赋初值 ** 下面的是声明:     extern i...

sysleo的博客 7867

AI Agent开发实战:100个真实场景题库解析

AI Agent开发涉及复杂的需求理解、架构设计及安全防护等关键技术环节。在工程实践中,开发者常面临需求偏差、系统崩溃等挑战,特别是在金融、医疗等高要求场景下。通过真实案例题库,开发者能系统性地掌握需求分析、架构决策树建模(如向量数据库选型)、安全攻防策略(如提示词注入防御)等核心技能。这些实战经验不仅能提升Agent的可用性和安全性,还能优化其在电商客服、智能家居等场景中的表现。题库采用动态难度系统,覆盖从基础到高级的开发挑战,是AI工程化落地的重要参考。

weixin_29057695的博客 354

error LNK2005:

<br /> <br />1. error LNK2005: "public: __thiscall CString::operator char const *(void)const " (??BCString@@QBEPBDXZ) already defined in Markup.objSolution:Project Settings -> C/C++,  Categories : Code generation -> Use run-time library: MultiThreaded 改为 D

oathevil的专栏 1641

c++11 无锁队列

/************************************************************************* &gt; File Name: lockfreequeue.cpp &gt; Author: &gt; Mail: &gt; Created Time: Wed 08 Aug 2018 11:38:08 AM CST ********...

qq_35728402的博客 5031

RK3568—基于GM8775C的MIPI转双通道LVDS屏幕调试

Rockchip RK3568 原生显示接口不支持双通道LVDS屏幕的数据输出,因此需要借助显示转换芯片才能实现双通道LVDS屏幕的驱动。本文介绍使用GM8775C芯片方案,在 RK3568 平台实现单路 MIPI DSI 信号输出来驱动双通道LVDS显示屏。 关于GM8775C GM8775C 型 DSI 转双通道 LVDS 发送器产品主要实现将 MIPI DSI 转单/双通道 LVDS功能,MIPI 支持 1/2/3/4 通道可选,每通道最高支持 1Gbps 速率,最大支持 4Gbps 速率。LV.

Industio_CSDN的博客 1万+

用VC写DLL中"error LNK2005: _DllMain@12 already defined"的解决办法

<br />可以使用 STL的string。   <br /> #include   <string>   <br /> using   namespace   std;<br /><br /> 但肯定不如CString功能强大,其实在非MFC程序中使用CString,也是可以的,具体方法如下:<br /><br />   为了使用CString类,首先源文件必须是以cpp结尾的,这是因为vc默认对不同的扩展名采用不同的编译方法和错误检查,mfc的支持文件Afx.h只有由cpp为扩展名的文件包含才能

henry_sea的专栏 890

“傻瓜”stata入门——熟悉操作界面

小白开始学习stata和matlab,零基础上手

nnpersistent的博客 4468

使用CString的方法(以及用VC写DLL中"error LNK2005: _DllMain@12 already defined"的解决办法)

可以使用 STL的string。   #include      using   namespace   std;但肯定不如CString功能强大,其实在非MFC程序中使用CString,也是可以的,具体方法如下:  为了使用CString类,首先源文件必须是以cpp结尾的,这是因为vc默认对不同的扩展名采用不同的编译方法和错误检查,mfc的支持文件Afx.h只有由cpp为扩展名的文件包含才能够正

DaLei的专栏 2380

深入RK3588嵌入式Linux开发:uboot源码定制化移植实战

本文详细介绍了RK3588嵌入式Linux开发中,如何绕过庞大的官方SDK,从官方仓库获取纯净U-Boot源码并进行定制化移植的完整实战流程。内容包括编译环境搭建、交叉工具链配置、源码结构解析、设备树(Device Tree)修改、环境变量定制、编译脚本编写以及常见问题调试技巧,旨在帮助开发者深入理解系统引导过程,掌握为定制硬件适配U-Boot的核心技能。

weixin_29169505的博客 429

晶振匹配电容容值计算方法

在使用晶振时,应仔细参考晶振规格书,以确保正确选择匹配电容,从而获得稳定和精准的时钟源。

aidi1111的博客 4665

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

编译出现如下错误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 obje

Gary's Blog --- A C++ programmer 3556

DeerFlow 系列教程 第四篇 | Sandbox 沙箱与 Sub-agent 子智能体协作详解

DeerFlow 系列教程第四篇介绍了两个关键子系统:Sandbox 沙箱系统和Sub-agent子智能体协作机制。沙箱系统通过虚拟路径映射、权限白名单、命令审计和容器隔离四重机制,为Agent提供安全的代码执行环境,使其能在隔离环境中执行文件操作和脚本运行。系统采用分层架构设计,包含应用层、工具层、审计层等,核心是定义标准交互接口的抽象基类。虚拟路径系统将Agent操作的虚拟路径映射到实际路径,确保安全性。这些功能使DeerFlow从对话助手进化为能处理复杂任务的Super Agent平台。

weixin_43674738的博客 293

error LNK2005: _DllMain@12 already defined in xxx.OBJ 解决办法

<br />用Visual C++编写DLL,如果在new project时选了MFC DLL,而后又想写成Regular DLL,即拥有自己的DllMain()入口函数,则在build时会遇到类似如下的link错误:<br /> error LNK2005: _DllMain@12 already defined in xxx.OBJ<br />几种解决方案:<br />1, 你只需要在工程设置里面,把<br />WIN32,NDEBUG,_WINDOWS,_MBCS,_USRDLL,MSGBOX_EXP

yqzsl的学习笔记 1万+

Unity 多渠道分包工程化实践(大白话版):从“手工打包”到“一键出 30 个包还不翻车”

Unity多渠道分包工程化实践摘要(150字) 本文系统探讨Unity多渠道包自动化构建方案,提出基于BuildPipeline的工程化体系。核心包括:1)通过ScriptableObject/JSON管理渠道参数(包名、签名、SDK配置等);2)结合命令行与环境变量实现CI集成;3)开发通用Builder脚本支持资源动态替换与参数注入。重点分享了Android渠道包的签名配置、图标自动替换、宏定义注入等关键技术实现,并给出Jenkins流水线集成案例。该方案可支持日均数百个渠道包的自动化构建,显著提升分发

本博客聚焦游戏开发技巧、创业实战心得及大学热门课程干货。这里既有技术深度,也有思维广度,无论你是开发者、创业者还是高校学子,都能在这里找到适合自己的成长之路! 282

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Project error LNK2005: _DllMain@12 already defined in MSVCRT.lib(dllmain.obj)

转自: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/0b154e1c-141f-4567-bb24-1ac7c8ee2713/ When upgrading a DLL from VC6->VS2005, and you get the following: mfcs80d.lib(dllmodul.obj) ...

qq_39459939的博客 580

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

用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

eulb的专栏 1万+

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

用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,N...

diaoqu4574的博客 292

error LNK2005: _DllMain@12 already defined in MSVC

<br />编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误。弄清楚它形成的原因,就可以轻松解决它了。   <br />    <br />  造成LNK2005错误主要有以下几种情况:   <br />  1.重复定义全局变量。可能存在两种情况:   <br /> A、对于一些初学编程的程序员,有时候会以为需要使 用全局变量的地方就可以使用定义申明一下。其实这是错误的,全局变量是针对整个工程的。正确的应该是在一个CPP文件中定义如下:int   g_Tes

oathevil的专栏 533

nafxcw.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined

如果开始用了一个ATL的Dll工程,然后为了在这个dll工程里面使用MFC,在里面加入MFC支持在stdafx.h的atlbase.h前面加入#include // MFC core and standard components#include // MFC extensions#include // MFC Automation extensions那么就会出现错误,例如nafxcw.l

求索 2805

: error LNK2005: _DllMain@12 already defined in dllmain.obj

用Visual C++编写DLL,如果在new project时选了MFC DLL,而后又想写成Regular DLL,即拥有自己的DllMain()入口函数,则在build时会遇到类似如下的link错误:  error LNK2005: _DllMain@12 already defined in xxx.OBJ 几种解决方案: 1, 你只需要在工程设置里面,把 WIN32,NDEBU

xum2008的专栏 6410

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

加粗样式

struggling_little_c的博客 758

error LNK2005: _DllMain@12 already defined in MSVCRTD.lib

原文:http://ticktick.blog.51cto.com/823160/571350本文主要分析和解决编译链接时产生的 LNK2005 错误。 【错误信息】: mfcs90ud.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj) 【...

diche7031的博客 180
上一篇: VC实现文件拖拽
下一篇: PacketGetAdapterNames返回false
sysprogram
博客等级 码龄17年 299粉丝 442原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址