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

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

 原创地址: http://blog.csdn.net/psusong/article/details/5858388

 用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)."

 

要解决此类问题,有三种解决方案:

 

1)只需要在工程设置里面,把WIN32,NDEBUG,_WINDOWS,_MBCS,_USRDLL,MSGBOX_EXPORTS,_WINDLL,_AFXDLL中的_USRDLL,删除,就可以正确编译了。

 

2)调整C runtime和MFC runtime的连接顺序,参见:http://support.microsoft.com/default.aspx?scid=kb;en-us;q148652

 

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

 

延伸阅读:

 

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

http://support.microsoft.com/kb/148791

2) Creating a DLL in DirectShow

http://msdn.microsoft.com/en-us/library/ms899461.aspx

http://msdn.microsoft.com/en-us/library/aa451266.aspx

3) http://www.codeguru.com/cpp/w-p/dll/article.php/c109/

4) DirectShow BaseClasses and MFC

http://blog.monogram.sk/janos/2008/01/10/directshow-baseclasses-and-mfc/

5) http://www.microsoft.com/whdc/driver/kernel/DLL_bestprac.mspx

相关文章推荐

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

转自:http://blog.csdn.net/psusong/article/details/5858388     用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

VC6.0,使用全局变量链接时出现LNK2005错误

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

http://support.microsoft.com/kb/148791/en-us Additional care must be taken when relying on DllMai...

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

今天,看到群上的群友问一个关于LINK2005的BUG,报:已经定义过的错误 其实这个问题难倒了很大一部分程序员,这个错误的原因是他想定义一个全局变量,于是他就在.h文件中直接定义并给这个变量赋值,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)