日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/4/18 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 error LNK2005: _DllMain@12 already defined的解决办法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

用Visual C++編寫DLL,如果在new project時選了MFC DLL,而后又想寫成Regular DLL,即擁有自己的DllMain()入口函數(shù),則在build時會遇到類似如下的link錯誤:

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

?

這是因為_USRDLL和_AFXDLL發(fā)生沖突所致。

?

幾種解決方案:

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

總結

以上是生活随笔為你收集整理的error LNK2005: _DllMain@12 already defined的解决办法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。