您的位置:首页 > 大数据 > 人工智能

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

2010-09-02 13:52 393 查看
用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: