visual-studio Visual Studio - 调试模式下的错误 LNK2005

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3885548/
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-10-22 12:33:59  来源:igfitidea点击:

Visual Studio - error LNK2005 in debug mode

visual-studiobuildlinker-errors

提问by Jonathan

I'm integrating 3rd party code into my MFC app under Visual Studio 2010.
When in Debug mode the following build error occurs:

我正在 Visual Studio 2010 下将第 3 方代码集成到我的 MFC 应用程序中。
在调试模式下,会发生以下构建错误:

1>LIBCMT.lib(invarg.obj) : error LNK2005: __initp_misc_invarg already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __call_reportfault already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __set_invalid_parameter_handler already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __get_invalid_parameter_handler already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: "void __cdecl _invoke_watson(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invoke_watson@@YAXPBG00II@Z) already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: __invalid_parameter already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: "void __cdecl _invalid_parameter(unsigned short const *,unsigned short const *,unsigned short const *,unsigned int,unsigned int)" (?_invalid_parameter@@YAXPBG00II@Z) already defined in libcmtd.lib(invarg.obj)
1>LIBCMT.lib(invarg.obj) : error LNK2005: ___pInvalidArgHandler already defined in libcmtd.lib(invarg.obj)
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>D:\My Documents\Dev\MyProject\MyProject\Debug\MyProject.exe : fatal error LNK1169: one or more multiply defined symbols found

Using this adviceI was able to complete the build in two ways:

使用这个建议,我能够以两种方式完成构建:

  • In Release mode
  • In Debug mode using /FORCE:MULTIPLE as an additional linker command line option
  • 在发布模式下
  • 在调试模式下使用 /FORCE:MULTIPLE 作为附加链接器命令行选项

In the second case (Debug mode) many warnings are still reported. If I also add /NODEFAULTLIB:LIBCMT most of them are gone.

在第二种情况下(调试模式),仍然报告了许多警告。如果我还添加 /NODEFAULTLIB:LIBCMT,它们中的大部分都消失了。

What is the cause of this?
How can I solve this, instead of working around it?

这是什么原因?
我该如何解决这个问题,而不是解决它?

回答by sblom

For some reason, you're linking against both LIBCMT and LIBCMTD (the debug version). (From reading the end of each error line: already defined in libcmtd.lib(invarg.obj))

出于某种原因,您同时链接到 LIBCMT 和 LIBCMTD(调试版本)。(通过阅读每个错误行的末尾:already defined in libcmtd.lib(invarg.obj)

You're fixing the right thing by saying /NODEFAULTLIB:LIBCMT. Does the debug/release flag on the third-party library that you're linking against match the debug/release mode on your app build? I would guess that the third-party code is pulling in a redundant library somehow.

您通过说 /NODEFAULTLIB:LIBCMT 来解决正确的问题。您链接的第三方库上的调试/发布标志是否与您的应用程序版本上的调试/发布模式匹配?我猜想第三方代码以某种方式引入了一个冗余库。

回答by yau

If you're lucky, your 3rd party package contains a xxx.lib as well as a xxxD.lib, like for LIBCMT. Then you would just have to link the appropriate one according to release/debug. Worked for me in a similar case.

如果幸运的话,您的第 3 方包包含 xxx.lib 和 xxxD.lib,就像 LIBCMT 一样。然后你只需要根据发布/调试链接适当的。在类似的情况下对我来说有效。

回答by Yura G

I resolved the same problem this way: In the Solution Explorer > Configuration Parameters > C/C++ > Code Generation. In the Runtime Library field, select.... for my "3rd party code" I had to select Multi-threaded (/MTd) and it worked.

我以这种方式解决了同样的问题:在解决方案资源管理器 > 配置参数 > C/C++ > 代码生成中。在运行时库字段中,为我的“第 3 方代码”选择...。我必须选择多线程 (/MTd) 并且它起作用了。