How I fixed “error LNK2005: _DllMain@12 already defined in msvcrtd.lib”

Today I got “error LNK2005: _DllMain@12 already defined in msvcrtd.lib” while linking some C++ CLI project with MFC support in MS Visual Studio 2013. As described in A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++ article, I added /verbose:lib linker option:

MS Visual Studio Linker options

and got the following linker output:

1>  Finished searching libraries
1>  
1>  Searching libraries
1>      Searching C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\lib\mfc120ud.lib:
1>      Searching C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\lib\mfcs120ud.lib:
1>mfcs120ud.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in msvcrtd.lib(dllmain.obj)
1>      Searching C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\msvcrtd.lib:
1>      Searching C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x86\kernel32.lib:

Obviously DllMain is defined twice in MFC and VC Runtime but that is all the information we can extract from here, so I did some further experimentations and finally removed couple files containing AFX_MANAGE_STATE(AfxGetStaticModuleState()) from the project, and it solved the problem. Looks like this AFX_MANAGE_STATE affects something that causes this linker error.

Also I got some annoying warning about HIMAGELIST “warning LNK4248: unresolved typeref token (01000018) for ‘_IMAGELIST’; image may not run”.  It is not clear what should I do with it, but at least useful links:

  1. Linker Tools Warning LNK4248
  2. warning LNK4248: unresolved typeref token (01000017) for ‘_TREEITEM’; image may not run

Leave a Reply

Your email address will not be published. Required fields are marked *