C++ 错误 LNK2005:_DllMain@12 已在 MSVCRT.lib 中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/343368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:47:27  来源:igfitidea点击:

error LNK2005: _DllMain@12 already defined in MSVCRT.lib

c++visual-c++linker

提问by mahesh

I am getting this linker error.

我收到此链接器错误。

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

mfcs80.lib(dllmodul.obj):错误 LNK2005:_DllMain@12 已在 MSVCRT.lib(dllmain.obj) 中定义

Please tell me the correct way of eliminating this bug. I read solution on microsoft support site about this bug but it didnt helped much.

请告诉我消除此错误的正确方法。我在微软支持网站上阅读了有关此错误的解决方案,但并没有太大帮助。

I am using VS 2005 with Platform SDK

我将 VS 2005 与 Platform SDK 一起使用

采纳答案by xtofl

If you read the linker error thoroughly, and apply some knowledge, you may get there yourself:

如果您彻底阅读链接器错误并应用一些知识,您可能会自己到达那里:

The linker links a number of compiled objects and libraries together to get a binary.

链接器将许多已编译的对象和库链接在一起以获得二进制文件。

Each object/library describes

每个对象/库描述

  • what symbols it expects to be present in other objects
  • what symbols it defines
  • 它期望在其他对象中出现什么符号
  • 它定义了什么符号

If two objects define the same symbol, you get exactly this linker error. In your case, both mfcs80.lib and MSVCRT.lib define the _DllMain@12 symbol.

如果两个对象定义了相同的符号,你就会得到这个链接器错误。在您的情况下, mfcs80.lib 和 MSVCRT.lib 都定义了 _DllMain@12 符号。

Getting rid of the error:

摆脱错误:

  1. find out which of both libraries you actually need
  2. find out how to tell the linker not to use the other one (using e.g. the tip from James Hopkin)
  1. 找出您真正需要的两个库中的哪一个
  2. 找出如何告诉链接器不要使用另一个(例如使用James Hopkin提示

回答by Constantin

I had the same error message, but none of the answers here solved it for me. So if you Encounter that Problem when creating a DLL Project that uses MFC, it can be resolved by entering the following line:

我有同样的错误消息,但这里的答案都没有为我解决。因此,如果您在创建使用 MFC 的 DLL 项目时遇到该问题,可以通过输入以下行来解决:

extern "C" { int _afxForceUSRDLL; } 

to the cpp file where DllMainis defined. Then your own DllMainimplementation is used, rather than the one from dllmain.obj.

DllMain定义的cpp文件。然后使用您自己的DllMain实现,而不是来自 dllmain.obj 的实现。

When we try to use MFC library, we surely will include afx.h directly or indirectly, 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 our program, because __afxForceUSRDLL is defined in dllmodule.cpp.

That's the common scenario. When we want to use our own DllMain in a mfc dll project, linker complains that there are two DllMain, one in our code, one in Dllmodule.obj.

So we need to tell the linker to add our dllmain.obj for __afxForceUSRDLL. So we need to 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.

当我们尝试使用 MFC 库时,我们肯定会直接或间接地包含 afx.h,然后 MFC(afx.h) 告诉链接器找到 __afxForceUSRDLL 的符号并将包含 __afxForceUSRDLL 的对象放入程序中,因此链接器搜索并将 dllmodule.obj 放入我们的程序中,因为 __afxForceUSRDLL 是在 dllmodule.cpp 中定义的。

这是常见的场景。当我们想在 mfc dll 项目中使用我们自己的 DllMain 时,链接器抱怨有两个 DllMain,一个在我们的代码中,一个在 Dllmodule.obj 中。

所以我们需要告诉链接器为 __afxForceUSRDLL 添加我们的 dllmain.obj。所以我们需要在自己的cpp文件中定义__afxForceUSRDLL,其中定义了我们自己的DllMain,然后链接器会忽略mfc的dllmodule.obj,只看到一个DllMain,从不抱怨。

Source: http://social.msdn.microsoft.com/Forums/en-US/0d78aa6b-1e87-4c01-a4a7-691335b7351a/how-to-build-mfc-application-dll-in-visual-c-2010

来源:http: //social.msdn.microsoft.com/Forums/en-US/0d78aa6b-1e87-4c01-a4a7-691335b7351a/how-to-build-mfc-application-dll-in-visual-c-2010

回答by James Hopkin

If you're defining your own DllMain, in your project settings you need to set 'Use of MFC' in the 'Configuration Properties/General' to 'Use Standard Windows Libraries'.

如果您要定义自己的 DllMain,则需要在项目设置中将“配置属性/常规”中的“使用 MFC”设置为“使用标准 Windows 库”。

You should do a clean rebuild after changing it.

您应该在更改后进行干净的重建。

回答by vmb100

In my project I was able to solve this problem by adding mfcs80.lib and msvcrt.lib as additional dependencies in the project settings. The 'additional dependencies' can be found under Linker -> Input.

在我的项目中,我能够通过在项目设置中添加 mfcs80.lib 和 msvcrt.lib 作为附加依赖项来解决这个问题。“附加依赖项”可以在链接器 -> 输入下找到。

In the debug configuration that would have to be mfcs80d.lib and msvcrtd.lib respectively.

在调试配置中,必须分别是 mfcs80d.lib 和 msvcrtd.lib。

By the way, I am working with Visual Studio 2010, so in my case the MFC lib is called mfc100.lib.

顺便说一下,我使用的是 Visual Studio 2010,所以在我的例子中,MFC 库被称为 mfc100.lib。

I am not sure why this worked. It is not necessary to add these lib files as additional dependencies because I already set 'Use of MFC' to 'Use MFC in a shared dll'. I guess that by specifying these libraries as additional dependencies they are linked in a different order.

我不确定为什么这有效。没有必要将这些 lib 文件添加为附加依赖项,因为我已经将“使用 MFC”设置为“在共享 dll 中使用 MFC”。我猜想通过将这些库指定为附加依赖项,它们以不同的顺序链接。

This solution is more or less the same as the one suggested on the Microsoft site: http://support.microsoft.com/kb/148652, except I did not need to type anything in the 'Ignore specific default libraries' box.

此解决方案与 Microsoft 站点上建议的解决方案或多或少相同:http: //support.microsoft.com/kb/148652,只是我不需要在“忽略特定默认库”框中键入任何内容。

回答by Ofek Shilon

For me the direct cause was indeed a missing _afxForceUSRDLL symbol reference, but the indirect cause was a missing _USRDLL macro definition. It is defined by default by the VC wizard, but occasionally devs erase it erroneously. Here it is in more words.

对我来说,直接原因确实是缺少 _afxForceUSRDLL 符号引用,但间接原因是缺少 _USRDLL 宏定义。默认情况下,它由 VC 向导定义,但有时开发人员会错误地将其擦除。 这里是更多的话

回答by Bill

MSDN knowledge base ID Q148652.

MSDN 知识库 ID Q148652。

http://support.microsoft.com/kb/148652

http://support.microsoft.com/kb/148652

Cause: Visual C++ compiles the source files in alphabetical order, and passes the compiled object files to the linker in alphabetical order. If the linker processes DLLDATAX.OBJ first, the source code references DllMain, which the linker loads from MSVCRTD.LIB(dllmain.obj). The linker then processes an object file compiled from a C++ file that contains #include "stdafx.h", which references the symbol __afxForceUSRDLL, which the linker loads from MFC42D.LIB(dllmodul.obj). This object module also contains an implementation for DllMain, causing the conflict.

原因:Visual C++ 按字母顺序编译源文件,并按字母顺序将编译的目标文件传递给链接器。如果链接器首先处理 DLLDATAX.OBJ,则源代码引用 DllMain,链接器从 MSVCRTD.LIB(dllmain.obj) 加载它。然后,链接器处理从包含 #include "stdafx.h" 的 C++ 文件编译的目标文件,该文件引用符号 __afxForceUSRDLL,链接器从 MFC42D.LIB(dllmodul.obj) 加载该符号。此对象模块还包含 DllMain 的实现,从而导致冲突。

回答by Carsten

For all those who are experiencing this error in ATL projects (mostly when trying to add MFC support), here's the solution I found after days of frustration!

对于所有在 ATL 项目中遇到此错误的人(主要是在尝试添加 MFC 支持时),这是我在几天的沮丧之后找到的解决方案!

First of all, this linkwas more helpful to me than all the others. It pointed me into the right direction. The problem occurs, if the "generated files" (containing the proxy and stub code, just as the type guids) for some reason have been removed and readded to the project. This causes Visual Studio to add them in the wrong order!

首先,这个链接比其他链接对我更有帮助。它为我指明了正确的方向。如果“生成的文件”(包含代理和存根代码,就像类型 guid)由于某种原因已被删除并重新添加到项目中,则会出现问题。这会导致 Visual Studio 以错误的顺序添加它们!

Usually you first come up with the "ATL requires C++ compilation" error, but you may have fixed this by turning out the Yc/Yu(precompiled headers) setting for that file.

通常,您首先会遇到“ATL 需要 C++ 编译”错误,但您可能已经通过Yc/Yu为该文件设置(预编译头)设置来解决此问题。

What you should do next is unloading your project and edit it. Search for the item groups that define the build and include order (ClCompileand ClInclude). Check their order and settings.

您接下来应该做的是卸载您的项目并对其进行编辑。搜索定义构建并包含订单(ClCompileClInclude)的项目组。检查它们的顺序和设置。

The compiles should appear in this order:

编译应按以下顺序出现:

  1. dllmain.cpp(with CompileAsManagedset to falseand PrecompiledHeaderleft empty).
  2. Library source (MyLib.cpp, containing DllCanUnloadNowand so on)
  3. Proxy/Stub code (MyLib_i.c; with same settings as dllmain.cpp)
  4. stdafx.cpp(with PrecompiledHeaderset to Create)
  5. All the other library source files (your actual libraries content)
  6. xdlldata.c(with the same settings as dllmain.cpp)
  1. dllmain.cppCompileAsManaged设置为falsePrecompiledHeader留空)。
  2. 库源(MyLib.cpp,包含DllCanUnloadNow等)
  3. 代理/存根代码(MyLib_i.c; 与 相同的设置dllmain.cpp
  4. stdafx.cppPrecompiledHeader设置为Create
  5. 所有其他库源文件(您的实际库内容)
  6. xdlldata.c(与 相同的设置dllmain.cpp

The includes should then be ordered like this:

然后应该像这样订购包含:

  1. dllmain.h
  2. MyLib_i.h
  3. Resource.h
  4. stdafx.h
  5. targetver.h
  6. ... (actual library headers)
  7. xdlldata.h
  1. dllmain.h
  2. MyLib_i.h
  3. Resource.h
  4. stdafx.h
  5. targetver.h
  6. ...(实际的库头文件)
  7. xdlldata.h

Fixing the build order fixed my project and I was able to create a new clean build.

修复构建顺序修复了我的项目,我能够创建一个新的干净构建。

回答by Matt Davis

Make sure you include "Stdafx.h" at the top of each .cpp file. I was getting the exact same error and had a single .cpp file that did not include this header at all. Adding the #include solved the problem.

确保在每个 .cpp 文件的顶部包含“Stdafx.h”。我得到了完全相同的错误,并且有一个完全不包含此标头的 .cpp 文件。添加#include 解决了问题。

回答by joseAndresGomezTovar

I have a very similar problem. [mfcs110d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj)] and the solution was add mfcs110d.lib to Additional Dependencies

我有一个非常相似的问题。[mfcs110d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj)] 解决方案是将 mfcs110d.lib 添加到附加依赖项

回答by joan

In my case I had a problem with the preprocessor directives. For some reason _USRDLLwas defined, when it should not have been.

就我而言,我的预处理器指令有问题。出于某种原因_USRDLL,它不应该被定义。

To check this, go to the menu Project , select Project Properties , then select the snippet Configuration Properties --> Preprocessor .

要检查这一点,请转到菜单Project ,选择Project Properties ,然后选择代码段Configuration Properties --> Preprocessor

The preprocessor directives will be found there.

预处理器指令将在那里找到。