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



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

转自:     用Visual C++编写DLL,如果在new project时选了MFC...

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

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

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

用Visual C++编写DLL,如果在new project时选了MFC DLL,而后又想写成Regular DLL,即拥有自己的DllMain()入口函数,则在build时会遇到类似如下的link...
  • yqzsl
  • yqzsl
  • 2011-02-14 10:46
  • 9348

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

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

全局变量引发的链接时的LNK2005xxx already defined in yyyy.obj


LNK2005 xxx already defined in libc.lib

原文地址:xxx already defined in libc.lib" style="text-decoration:none; color:rgb(106,63,48)">LNK2005 xxx...

error LNK2005:XXXX already defined in XXX.lib

分类: 编译器VC Compile Error  Linking...base64.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to ...

HOWTO: How to Provide Your Own DllMain in an MFC Regular DLL Additional care must be taken when relying on DllMai...

关于error LNK2005: "char * xxx" (?xx@@3PADA) already defined in xxx

今天,看到群上的群友问一个关于LINK2005的BUG,报:已经定义过的错误 其实这个问题难倒了很大一部分程序员,这个错误的原因是他想定义一个全局变量,于是他就在.h文件中直接定义并给这个变量赋值,...