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

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

  1. Kurman says:

    Hi Guys, I got same frustrating problem which raised suddenly in already working project ( In VS 2017).Same as above:
    mfcs140d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in msvcrtd.lib(dllmain.obj)
    My solution was simple – because problem was caused by wrong order in linkers library search, i added the mfcs140d.lib to “Additional Dependencies” field in “Project Property” tab.

Leave a Reply

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