Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error LNK2005: _DllMain@12 already defined in MSVCRT.lib

I am getting this linker error.

mfcs80.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRT.lib(dllmain.obj)

Please tell me the correct way of eliminating this bug. I read solution on microsoft support site about this bug but it didnt helped much.

I am using VS 2005 with Platform SDK

like image 748
Mahesh Avatar asked Dec 05 '08 10:12

Mahesh


3 Answers

I had the same error message, but none of the answers here solved it for me. So if you Encounter that Problem when creating a DLL Project that uses MFC, it can be resolved by entering the following line:

extern "C" { int _afxForceUSRDLL; } 

to the cpp file where DllMain is defined. Then your own DllMain implementation is used, rather than the one from dllmain.obj.

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.

Source: http://social.msdn.microsoft.com/Forums/en-US/0d78aa6b-1e87-4c01-a4a7-691335b7351a/how-to-build-mfc-application-dll-in-visual-c-2010

like image 128
Constantin Avatar answered Nov 15 '22 07:11

Constantin


If you read the linker error thoroughly, and apply some knowledge, you may get there yourself:

The linker links a number of compiled objects and libraries together to get a binary.

Each object/library describes

  • what symbols it expects to be present in other objects
  • what symbols it defines

If two objects define the same symbol, you get exactly this linker error. In your case, both mfcs80.lib and MSVCRT.lib define the _DllMain@12 symbol.

Getting rid of the error:

  1. find out which of both libraries you actually need
  2. find out how to tell the linker not to use the other one (using e.g. the tip from James Hopkin)
like image 16
xtofl Avatar answered Nov 15 '22 07:11

xtofl


If you're defining your own DllMain, in your project settings you need to set 'Use of MFC' in the 'Configuration Properties/General' to 'Use Standard Windows Libraries'.

You should do a clean rebuild after changing it.

like image 11
James Hopkin Avatar answered Nov 15 '22 08:11

James Hopkin