Can't get by "DllMain already defined" error

19,545

Solution 1

Well, I guess I threw in the towel on this one (sort of). I was able to at least get by all my problems. I just had to stop using some of the Microsoft classes.

I touched on this in the problem description, but I recall starting to have difficulty with compiling as soon as I started including:

#include <afxmt.h>
#include <afxwin.h>

So I went through and figured out what exactly I was using that required these includes. I was using the AfxBeginThread() method, and the classes CMutex and CCriticalSection. So I figured maybe if I could just get away from any of the proprietary windows stuff that maybe my problems would go away. That means removing all includes of , , and and then address the compilation errors with more standard c++ code. Here is what I did:

  • Instead of using AfxBeginThread() I used CreateThread().
  • Instead of using CMutex and CCriticalSection I used the CRITICAL_SECTION structure with its accompanying routines.

After this I was able to compile the .dll and it worked fine.

Solution 2

In many cases this is caused by having _USRDLL in the preprocessor settings, where it should be _LIB. This has to do with 'MFC extension dlls' which I don't think anyone still makes today, yet the VS wizard seems to assume you do want to use this when you check 'Use MFC' in the wizard.

Solution 3

Recently, I experienced the same or a similar issue, and found a solution.

Background

I have an MFC project in Visual Studio 2013 Pro, which generates a DLL. I have several .c modules in the project, which I'm able to do by conditionally specifying the 'extern "C"' construct, disabling precompiled headers for those C files, and - in my case - disabling inherited forced includes, which was pulling-in stdafx.h from the project defaults.

Problem

One day, after having used this method successfully on several C files, when I'd try to add just one more, I'd get the following error.

1>Link:
1>  All outputs are up-to-date.
1>mfcs120d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj)
1>     Creating library C:\path\to\project\build_dir\myproj.lib and object C:\path\to\project\build_dir\myproj.exp
1>C:\path\to\project\build_dir\myproj.dll : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.

Solution

I resolved this by implementing "Solution One" from Microsoft Knowledge Base article Q148652, "A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++". This forces the linker to link the libraries in the correct order.

Steps:

  1. Right-click the project, choose Properties.
  2. Ensure you're making changes for All Configurations, and All Platforms.
  3. In the left pane, browse to Linker → Input.
  4. In the right pane, pull down Additional Dependencies, choose <Edit...>.
  5. Add mfcs120d.lib.

Based on the Code Project article Solve error LNK2005: _DllMain@12 already defined in msvcrtd.lib(dllmain.obj) in MFC Projects", I figure I might have to add another library to that list someday, but this much works for me for now.

Solution 4

I got the error when I have moved #include afxdllx.h from dllmain.cpp to StdAfx.h. My project works without this include also

Share:
19,545
Ultratrunks
Author by

Ultratrunks

Updated on July 30, 2022

Comments

  • Ultratrunks
    Ultratrunks almost 2 years

    I'm trying to write a .dll library for .dll injection purposes. And because of this fact, it must have a routine called DllMain, since this is what will be used as the entry point. I think my problem may be stemming from the fact that I'm linking in a static library that I've wrote which utilizes a threads and mutexes from afxmt.h. Because somewhere down the line, the inclusion of this is causing the linker to link from mfcs100ud.lib which apparently contains its own version of DllMain.

    Here is the file that is giving me trouble:

    dllmain.cpp

    #include "stdafx.h"
    #include <stdio.h>
    #include "NamedPipeLogger.h"
    
    static CNamedPipeLogger m_PipeLogger("Log.txt");
    
    BOOL APIENTRY DllMain(HANDLE hModule, 
                          DWORD  ul_reason_for_call, 
                          LPVOID lpReserved)
    {
    }
    

    Here is the stdafx.h file that dllmain.cpp is including.

    stdafx.h

    #pragma once
    
    #define _AFXDLL
    #include <Afx.h>
    
    #include "targetver.h"
    
    #define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
    

    Here is my Error message:

    Error 32 error LNK2005: _DllMain@12 already defined in dllmain.obj D:\xxxxx\xxxxx\xxxxxx\mfcs100ud.lib(dllmodul.obj)

    Am I just screwed here because I cannot change the name of my Dll entry point to something other than DllMain?

  • zwol
    zwol about 12 years
    I am laughing at the notion that CreateThread and CRITICAL_SECTION are "more standard" than what you were using. Nothing you are doing is standard; it is Windows-specific from A to Z. (There is nothing wrong with that, but you should be aware of it.)
  • Ultratrunks
    Ultratrunks about 12 years
    Well it doesn't require Application Framework eXtention headers (afx), which was given me trouble with mysterious dlls during link time. That's what I was getting at.