How to resolve '_DllMain@12 already defined in xxx.obj' ?

转载 2012年03月27日 11:45:21


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


1>uafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in xxx.obj


此种连接错误在用DirectShow + MFC实现filter的时候必然会遇到:


"If you try to use MFC framework classes (CWinAapp-, CWnd- derived) in your AX filters, you end up with tons of error messages from the not-initialized MFC framework. To fix this several changes need to be done in the initialization functions (dllentry.cpp)."






2)调整C runtime和MFC runtime的连接顺序,参见:;en-us;q148652




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; }


    extern “C” { int __afxForceUSRDLL; }



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:



    // force inclusion of DLLMODUL.OBJ for _USRDLL

    #ifdef _USRDLL

    #pragma comment(linker, “/include:__afxForceUSRDLL”)



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




1) How to Provide Your Own DllMain in an MFC Regular DLL

2) Creating a DLL in DirectShow


4) DirectShow BaseClasses and MFC



主应用程序中引用了外部的Module模块,出现了如下错误,不保证如下解决方法通用所有问题:1: Error:(2) Error retrieving parent for item: No resou...
  • xueshanhaizi
  • xueshanhaizi
  • 2016年03月16日 19:38
  • 1772


摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码。   使用方式:...
  • xiangxizhishi
  • xiangxizhishi
  • 2018年01月15日 08:00
  • 24

使用 symbolicatecrash 解析崩溃堆栈

  • duanyipeng
  • duanyipeng
  • 2015年06月11日 17:53
  • 4848


1、环境准备(1)下载安装包并解压至ORACLE_BASE(2)修改环境变更[oracle@irms156ogg]$ vi ~/.bashrc exportPATH=$ORACLE_HOME/bin:...
  • jediael_lu
  • jediael_lu
  • 2013年05月16日 10:48
  • 3023

关于Fragment 不响应onActivityResult的情况分析

大家都知道,可以通过使用 startActivityForResult() 和 onActivityResult() 方法来传递或接收参数。 但你是否遭遇过onActivityResult()不...
  • ljz2009y
  • ljz2009y
  • 2014年09月17日 15:18
  • 2459


  • sinat_35515778
  • sinat_35515778
  • 2017年05月24日 16:12
  • 5092


  • axi295309066
  • axi295309066
  • 2016年10月06日 21:23
  • 4962


NetBeans部署项目(Extjs)报错(二) 1、具体错误如下: Using CATALINA_BASE: "C:\Users\Administrator.FOXB2MKB3RGUNIL...
  • you23hai45
  • you23hai45
  • 2014年03月27日 23:40
  • 1856


(mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION); break; } } 十五、改变应用程序的图标 缺 十六、...
  • yu704645129
  • yu704645129
  • 2014年10月29日 09:57
  • 2407


前面介绍UDT基础开发,这里介绍怎么使用UDT的多Target对应不同模型的问题。  对于UDT的示例,我觉得有必要把这个类单独拿出来讲一下,  UserDefinedTargetEventH...
  • han15939423461
  • han15939423461
  • 2017年06月10日 14:08
  • 308
您举报文章:How to resolve '_DllMain@12 already defined in xxx.obj' ?