_DllMain@12 already defined in MSVCRTD.lib

来源:互联网 发布:淘宝客服是什么意思 编辑:程序博客网 时间:2024/05/17 20:11

   本文主要分析和解决编译链接时产生的 LNK2005 错误。

【错误信息】:

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

【分析原因】:

     简单地说,就是产生错误的工程同时使用了CRT库和MFC库,并且链接顺序不对,CRT 库对 new、delete 和 DllMain 函数使用弱外部链接。MFC 库也包含 new、delete 和 DllMain 函数。这些函数要求先链接 MFC 库,然后再链接 CRT 库。

【解决方法一】:

     在“项目 -> 属性 -> 链接器 -> 命令行”栏中,输入 /verbose:lib编译参数,这样,就可以在output窗口看到链接时搜索的库的先后顺序,对于本问题,你一定可以看到类似下面的先后输出: 
  1. >Searching D:\vs2008\VC\lib\MSVCRTD.lib: 
  2. >Searching D:\vs2008\VC\atlmfc\lib\mfcsxxx.lib: 
    由此可以看出,默认情况下,先链接了CRT库MSVCRTD.lib,然后链接的是MFC的库mfcsxxx.lib,因此,需要强制改变一下它们的链接顺序。
    选择“项目 -> 属性 -> 链接器 -> 输入”栏,在其中的“忽略特定的库”这一栏输入:MSVCRTD.libmfcsxxx.lib, 然后,在“附加库”这一栏中输入:mfcsxxx.lib MSVCRTD.lib  问题即可解决。

【解决方法二】:
该情况是基于工程配置为 MDd  和 use  MFC in Share DLL 如果项目需要使用 use MFC in static library
则需要在项目的 #include<stdfx.h> 前面加上# define _AFXDLL

In file StdAfx.cpp 

#define  _AFXDLL before #include "stdafx.h" like this 

#define _AFXDLL // this is now the first item in the file.
#include "stdafx.h"

阅读全文
0 0