How to resolve '_DllMain@12 already defined in xxx.obj' ?


    用Visual C++编写DLL,如果在new project时选了MFC DLL,而后又想写成Regular DLL,即拥有自己的DllMain()入口函数,则在build时会遇到类似如下的link错误:


1>uafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in xxx.obj


此种连接错误在用DirectShow + MFC实现filter的时候必然会遇到:


"If you try to use MFC framework classes (CWinAapp-, CWnd- derived) in your AX filters, you end up with tons of error messages from the not-initialized MFC framework. To fix this several changes need to be done in the initialization functions (dllentry.cpp)."






2)调整C runtime和MFC runtime的连接顺序,参见:;en-us;q148652




I want to add some initial code in DllMain in a MFC dll project, but after I added the code and compiled, there was a link error:


mfcs42d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in DLLMAIN.OBJ


DLLMAIN.cpp is the file I created by my own and I define DllMain() in it.


What’s the reason? the linker complains that I have a DllMain in DLLMAIN.cpp but there’s another DllMain in mfcs42d.lib.


So how to use my own DllMain if a MFC dll project? There’s a quick answer on codeguru , but that article just show the tip without explaining it with more details.


The article says, just copy MFC’s dllmodule.cpp into your own project and compile, it will be OK. It seems to be nonsense but after I tried I found it works. But why? By commenting out unnecessary lines, I find these lines are the key point: //(dllmodule.cpp)


    // The following symbol used to force inclusion of this module for _USRDLL 

    #ifdef _X86_

    extern “C” { int _afxForceUSRDLL; }


    extern “C” { int __afxForceUSRDLL; }



Do you noticed the comment? it forces the inclusion of the module of dllmodule.obj. But how? A searching for _afxForceUSRDLL in MFC source code gives me the answer:



    // force inclusion of DLLMODUL.OBJ for _USRDLL

    #ifdef _USRDLL

    #pragma comment(linker, “/include:__afxForceUSRDLL”)



Again the MFC designer gives us a good comment: “force inclusion of DLLMODUL.OBJ”, OK, got it.


Now let summary it up:

    1)When you try to use MFC library, you surely will include afx.h directly or indirectly 

    2)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 my program, for __afxForceUSRDLL is defined in dllmodule.cpp That’s the common scenario.


Then you want to use your own DllMain in a mfc dll project, linker complains that there are two DllMain, one in your code, one in Dllmodule.obj.


The solution? Tell the linker to add my dllmain.obj for __afxForceUSRDLL. So we 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 the solution is just to add extern “C” { int _afxForceUSRDLL; } in the file where your own DllMain is defined, copying mfc’s dllmodule.cpp is not necessary :-)




1) How to Provide Your Own DllMain in an MFC Regular DLL

2) Creating a DLL in DirectShow


4) DirectShow BaseClasses and MFC



The type Employee is already defined?


[code=Java]package com.test.input;rnrnpublic class ParamTest rnrn public static void main(String[] args) rnrn System.out.println("Testing tripleValue"); rn double percent = 10; rn System.out.println("Before: percent="+percent); rn tripleValue(percent); rn System.out.println("After: percent="+percent); rn System.out.println("\nTesting tripleSalary:"); rn Employee harry = new Employee("Harry",50000); rn System.out.println("Before: salary="+harry.getSalary()); rn tripleSalary(harry); rn System.out.println("After: salary="+harry.getSalary()); rn System.out.println("\nTesting swap:"); rn Employee a = new Employee("Alice",70000); rn Employee b = new Employee("Bob",60000); rn System.out.println("Before: a = "+a.getName()+" "+"b = "+b.getName()); rn swap(a,b); rn System.out.println("After: a = "+a.getName()+" "+"b = "+b.getName()); rn rn rn rn public static void tripleValue(double x) rn rn x=3*x; rn System.out.println("End fo method: x=" +x); rn rn public static void tripleSalary(Employee x) rn rn x.raiseSalary(200); rn System.out.println("End of method: salary ="+x.getSalary()); rn rn rn public static void swap(Employee x,Employee y) rn rn Employee temp = x; rn x = y; rn y = temp; rn System.out.println("End of method: x="+x.getName()+" y="+y.getName()); rn rn rnrn class Employee //The type Employee is already defined.rn rn public Employee (String n, double s) rn rn name = n; rn salary = s; rn rn public String getName() rn rn return name; rn rn public double getSalary() rn rn return salary; rn rn rn public void raiseSalary (double byPercent) rn rn double raise = salary*byPercent/100; rn salary +=raise; rn rn private String name; rn private double salary; rnrn [/code]