locked
how to build mfc application dll in visual c++ 2010 RRS feed

  • Question

  • hello everyone

    in my visual c++ 2010 project i used mfc class and when i compile my project than it will throws  error LNK2005: DllMain already defined in DllEntry.obj    C:\Users\admin\Documents\Visual Studio 2010\Projects\utilityfileexpo\utilityfileexpo\mfcs100u.lib(dllmodul.obj) mean mfc dll and my project dll both execute so compiler throws this error now i want to compile both task on single dll if anybody knows than help me?


    Tushar Kachhadiya

    Wednesday, September 18, 2013 4:13 PM

Answers

  • Hi,

    According to your description, I think that you created a DLL project with MFC support. If my understanding is right, now the issue is how to use our own DllMain when we create a MFC dll project.

    When we try to use MFC library, we surely will include afx.h directly or indirectly, 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 our program, because __afxForceUSRDLL is defined in dllmodule.cpp.  

    That’s the common scenario. When we want to use our own DllMain in a mfc dll project, linker complains that there are two DllMain, one in our code, one in Dllmodule.obj.

    So we need to tell the linker to add our dllmain.obj for __afxForceUSRDLL. So we need to 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 my suggestion is to add extern “C” { int _afxForceUSRDLL; } in the file where your own DllMain is defined.

    Hope this can help you.

    Best Regards,

    May


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, September 19, 2013 6:27 AM