简体   繁体   English

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

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

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 一起使用

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 DllMain is defined.到定义DllMain的 cpp 文件。 Then your own DllMain implementation is used, rather than the one from dllmain.obj.然后使用您自己的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.当我们尝试使用 MFC 库时,我们肯定会直接或间接地包含 afx.h,然后 MFC(afx.h) 告诉链接器找到 __afxForceUSRDLL 的符号并将包含 __afxForceUSRDLL 的对象放入程序中,因此链接器搜索并将 dllmodule.obj 放入我们的程序中,因为 __afxForceUSRDLL 是在 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.当我们想在 mfc dll 项目中使用我们自己的 DllMain 时,链接器抱怨有两个 DllMain,一个在我们的代码中,一个在 Dllmodule.obj 中。

So we need to tell the linker to add our dllmain.obj for __afxForceUSRDLL.所以我们需要告诉链接器为 __afxForceUSRDLL 添加我们的 dllmain.obj。 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.所以我们需要在自己的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

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 eg the tip from James Hopkin )找出如何告诉链接器不要使用另一个(例如使用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.您应该在更改后进行干净的重建。

In my project I was able to solve this problem by adding mfcs80.lib and msvcrt.lib as additional dependencies in the project settings.在我的项目中,我能够通过在项目设置中添加 mfcs80.lib 和 msvcrt.lib 作为附加依赖项来解决这个问题。 The 'additional dependencies' can be found under Linker -> Input. “附加依赖项”可以在链接器 -> 输入下找到。

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'.没有必要将这些 lib 文件添加为附加依赖项,因为我已经将“使用 MFC”设置为“在共享 dll 中使用 MFC”。 I guess that by specifying these libraries as additional dependencies they are linked in a different order.我猜想通过将这些库指定为附加依赖项,它们以不同的顺序链接。

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 ,除了我不需要在“忽略特定默认库”框中键入任何内容。

For me the direct cause was indeed a missing _afxForceUSRDLL symbol reference, but the indirect cause was a missing _USRDLL macro definition.对我来说,直接原因确实是缺少 _afxForceUSRDLL 符号引用,但间接原因是缺少 _USRDLL 宏定义。 It is defined by default by the VC wizard, but occasionally devs erase it erroneously.默认情况下,它由 VC 向导定义,但有时开发人员会错误地将其擦除。Here it is in more words .这里是更多的话

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.原因:Visual C++ 按字母顺序编译源文件,并按字母顺序将编译的目标文件传递给链接器。 If the linker processes DLLDATAX.OBJ first, the source code references DllMain, which the linker loads from MSVCRTD.LIB(dllmain.obj).如果链接器首先处理 DLLDATAX.OBJ,则源代码引用 DllMain,链接器从 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).然后,链接器处理从包含 #include "stdafx.h" 的 C++ 文件编译的目标文件,该文件引用符号 __afxForceUSRDLL,链接器从 MFC42D.LIB(dllmodul.obj) 加载该符号。 This object module also contains an implementation for DllMain, causing the conflict.此对象模块还包含 DllMain 的实现,从而导致冲突。

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 添加到附加依赖项

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 link was 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.如果“生成的文件”(包含代理和存根代码,就像类型 guid)由于某种原因已被删除并重新添加到项目中,则会出现问题。 This causes Visual Studio to add them in the wrong order!这会导致 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 ( ClCompile and ClInclude ).搜索定义构建和包含顺序的项目组( ClCompileClInclude )。 Check their order and settings.检查它们的顺序和设置。

The compiles should appear in this order:编译应按以下顺序出现:

  1. dllmain.cpp (with CompileAsManaged set to false and PrecompiledHeader left empty). dllmain.cppCompileAsManaged设置为false并且PrecompiledHeader留空)。
  2. Library source ( MyLib.cpp , containing DllCanUnloadNow and so on)库源( MyLib.cpp ,包含DllCanUnloadNow等)
  3. Proxy/Stub code ( MyLib_i.c ; with same settings as dllmain.cpp )代理/存根码( MyLib_i.c ;具有相同的设置dllmain.cpp
  4. stdafx.cpp (with PrecompiledHeader set to Create ) stdafx.cpp (将PrecompiledHeader设置为Create
  5. All the other library source files (your actual libraries content)所有其他库源文件(您的实际库内容)
  6. xdlldata.c (with the same settings as dllmain.cpp ) 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

Fixing the build order fixed my project and I was able to create a new clean build.修复构建顺序修复了我的项目,我能够创建一个新的干净构建。

I have personally got rid of this error this way: right-clicked project in the Solution Explorer , selected Properties from pop-up menu, clicked Linker tab and added mfcs71ud.lib into Additional Dependencies .我个人通过这种方式摆脱了这个错误:右键单击Solution Explorer项目,从弹出菜单中选择Properties ,单击Linker选项卡并将mfcs71ud.lib添加到Additional Dependencies If you're using Visual Studio 2005, it should be "80" instead of "71" and so on.如果您使用的是 Visual Studio 2005,它应该是“80”而不是“71”等等。

In my case I had a problem with the preprocessor directives.就我而言,我的预处理器指令有问题。 For some reason _USRDLL was 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.预处理器指令将在那里找到。

Just #undef the _USRDLL before including afx.h , or even better, edit your project configuration and remove the macro.在包含afx.h之前,只需#undef _USRDLL ,或者更好的是,编辑您的项目配置并删除宏。

This is the usual configuration for a MFC extension DLL: Build Settings for an MFC DLL这是 MFC 扩展 DLL 的常用配置:MFC DLL 的构建设置

Make sure you include "Stdafx.h" at the top of each .cpp file.确保在每个 .cpp 文件的顶部包含“Stdafx.h”。 I was getting the exact same error and had a single .cpp file that did not include this header at all.我得到了完全相同的错误,并且有一个完全不包含此标头的 .cpp 文件。 Adding the #include solved the problem.添加#include 解决了问题。

I found solution Here Visual Studio 2010 library linking order我在这里找到了解决方案Visual Studio 2010 library linking order

this is: /FORCE:MULTIPLE in linker options这是:链接器选项中的 /FORCE:MULTIPLE

I had to mix ATL and MFC together , to use [module(name = "mymodule")];我不得不将 ATL 和 MFC 混合在一起,以使用 [module(name = "mymodule")]; construction in MFC application together with "__hook" keyword在 MFC 应用程序中与“__hook”关键字一起构建

I found this which helped me: http://support.microsoft.com/kb/148652我发现这对我有帮助: http : //support.microsoft.com/kb/148652

Basically the linker order was incorrect.基本上链接器顺序不正确。 the CRT libs were getting linked before the MFC libs. CRT 库在 MFC 库之前被链接。 Turns out, the MFC libs had to get linked FIRST, and then the CRT libs could be linked.事实证明,必须首先链接 MFC 库,然后才能链接 CRT 库。

Yucko Microsoft!!优子微软!!

Project Properties -> Linker Tab -> Input of Visual StudioAdditional Dependancies字段中声明mfc80ud.libmfcs80ud.lib以解决问题。

There is a common theme running through some of the answers here.这里的一些答案有一个共同的主题。

Avishek Bose:- Avishek Bose:-

Declare the mfc80ud.lib and mfcs80ud.lib in the Additional Dependancies field in the Project Properties -> Linker Tab -> Input of Visual Studio to fix the issue.在 Project Properties -> Linker Tab -> Input of Visual Studio 的 Additional Dependancies 字段中声明 mfc80ud.lib 和 mfcs80ud.lib 以解决问题。

vmb100:- vmb100:-

I am working with Visual Studio 2010, so in my case the MFC lib is called mfc100.lib.我正在使用 Visual Studio 2010,所以在我的情况下,MFC 库被称为 mfc100.lib。

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 添加到附加依赖项

So the general case seems to be to first find the name of the library to add ...所以一般的情况好像是先找到库名来添加...

图书馆名称

and then to add it ....然后添加它....

将库添加到依赖项

Note that there seem to be some prerequisites and/or alternative solutions.请注意,似乎有一些先决条件和/或替代解决方案。

This can also occur if your solution has multiple projects that export the same symbols.如果您的解决方案有多个导出相同符号的项目,也会发生这种情况。 For example if you have sub-project that builds foo.dll with a foo.def file that exports DoFoo and a sub-project for bar.dll with a bar.def file that exports DoFoo , a collision will occur and this is the error you will see when you link.例如,如果您有子项目,建立foo.dllfoo.def文件出口DoFoo子项目bar.dll与bar.def文件出口DoFoo ,会发生冲突,这是错误你会在链接时看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CUDA:错误LNK2005:MSVCRT.lib中已经定义了DllMain - CUDA: error LNK2005: DllMain already defined in MSVCRT.lib 错误22错误LNK2005:_realloc已在libcmt.lib(realloc.obj)MSVCRT.lib中定义 - Error 22error LNK2005: _realloc already defined in libcmt.lib(realloc.obj) MSVCRT.lib 什么导致VS2013错误LNK2005:__xi_a已在MSVCRT.lib(cinitexe.obj)中定义? - What is causing VS2013 error LNK2005: __xi_a already defined in MSVCRT.lib(cinitexe.obj)? 错误LNK2005:MSVCRT.lib(MSVCR100.dll)中已定义的xxx C:\\ something \\ LIBCMT.lib(setlocal.obj) - error LNK2005: xxx already defined in MSVCRT.lib(MSVCR100.dll) C:\something\LIBCMT.lib(setlocal.obj) 错误 LNK2005 DllMain 已在 uafxcw.lib(dllmodul.obj) 中定义 - Error LNK2005 DllMain already defined in uafxcw.lib(dllmodul.obj) 使用 MSVC 的 Linker 中的“LNK2005 DLLMain 已定义...”冲突 - "LNK2005 DLLMain already defined ..." conflict in Linker using MSVC 错误LNK2005构造函数已定义 - error LNK2005 constructor already defined 错误 lnk2005 已在 .obj 中定义 - Error lnk2005 already defined in .obj 错误LNK2005对象已定义 - Error LNK2005 object already defined LNK2005:“已定义错误 - LNK2005: " already defined error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM