C++ 将配置从调试更改为发布时出现链接错误

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

link error when change configuration from Debug to Release

c++compiler-errors

提问by herzl shemuelian

Hi I have project In VC++ 2008 this project compile in debug mode without error but when I try to build it in release mode I get below linking erorr.

嗨,我有项目在 VC++ 2008 中,这个项目在调试模式下编译没有错误,但是当我尝试在发布模式下构建它时,我得到以下链接错误。

1>Linking...
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __heap_alloc already defined in LIBCMT.lib(malloc.obj)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __recalloc already defined in LIBCMT.lib(recalloc.obj)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __msize already defined in LIBCMT.lib(msize.obj)
1>LIBCMTD.lib(malloc.obj) : error LNK2005: _V6_HeapAlloc already defined in LIBCMT.lib(malloc.obj)
1>LIBCMTD.lib(dbghook.obj) : error LNK2005: __crt_debugger_hook already defined in LIBCMT.lib(dbghook.obj)
1>LIBCMTD.lib(sbheap.obj) : error LNK2005: ___sbh_pHeaderDefer already defined in LIBCMT.lib(sbheap.obj)
1>LIBCMTD.lib(sbheap.obj) : error LNK2005: __get_sbh_threshold already defined in LIBCMT.lib(sbheap.obj)
1>LIBCMTD.lib(sbheap.obj) : error LNK2005: __set_sbh_threshold already defined in LIBCMT.lib(sbheap.obj)
1>LIBCMTD.lib(sbheap.obj) : error LNK2005: __set_amblksiz already defined in LIBCMT.lib(sbheap.obj)
1>LIBCMTD.lib(sbheap.obj) : error LNK2005: __get_amblksiz already defined in LIBCMT.lib(sbheap.obj)......

how I can fix it? thanks.

我该如何解决?谢谢。

回答by Erik

You're mixing VS runtimes - Make sure the "Project Properties/C++/Code Generation/Run-time Library" setting is the same for your project and all libraries you link to.

您正在混合 VS 运行时 - 确保您的项目和您链接到的所有库的“项目属性/C++/代码生成/运行时库”设置相同。

回答by Zdeno Pavlik

I had the same problem as you. Reason of this problem is that Linker include a lot of libraries that you dont see (for example you will include windows.h, it includes windef.hand so on...). And because these files are not idiot-proof written (#ifndefis missing) linker will try to #definesame thing more times than once - problem.

我和你有同样的问题。出现此问题的原因是 Linker 包含许多您看不到的库(例如,您将包含windows.h,它包含windef.h等等...)。并且因为这些文件不是白痴编写的(#ifndef丢失)链接器将#define多次尝试相同的事情 - 问题。

My solution was setting this field: Project Properties -> Linker -> Input -> Ignore specific library

我的解决方案是设置此字段:项目属性 -> 链接器 -> 输入 -> 忽略特定库

to "libcmt.lib"

到“ libcmt.lib

With this your linker will ignore also in release configurationthat problematic library that is included by default and everything should work.. :-)

有了这个,你的链接器也会在发布配置中忽略默认包含的有问题的库,一切都应该正常工作..:-)

Have a nice day.. :-)

祝你今天过得愉快.. :-)

P.S. If you want to avoid these linker misunderstandings, keep configuring project not in debug or release configuration, but in "All configuration". This option is on left upper corner of project configuration.

PS 如果您想避免这些链接器误解,请不要在调试或发布配置中配置项目,而是在“所有配置”中配置项目。该选项位于项目配置的左上角。

回答by Hans Passant

You are linking a .obj or .lib file that was compiled in the Debug configuration. Best thing to do is to ensure that file is built in the Release mode as well. Second best is to tell the linker to ignore the request to link to libcmtd.lib. Project + Properties, Linker, Input, Ignore Specific Library = libcmtd.lib. But only do this if you can't get the Release build of a .lib after trying mightily.

您正在链接在调试配置中编译的 .obj 或 .lib 文件。最好的办法是确保文件也是在发布模式下构建的。其次是告诉链接器忽略链接到 libcmtd.lib 的请求。项目 + 属性、链接器、输入、忽略特定库 = libcmtd.lib。但只有在尝试了很多之后还是无法获得 .lib 的发布版本时才这样做。

回答by Julien

you can delete preprocessor definition "DEBUG" to solve your issue

您可以删除预处理器定义“DEBUG”来解决您的问题

回答by Hans

I ran into to similar problem and it was all to do with whether libraries you include build with right end state. If you build a program in release mode and pointed to *.lib files that compiled in debug mode. its going to give you symbol miss match errors. Specially if your program build fines in debug mode or vise versa.

我遇到了类似的问题,这完全与您包含的库是否以正确的最终状态构建有关。如果您在发布模式下构建程序并指向在调试模式下编译的 *.lib 文件。它会给你符号未命中匹配错误。特别是如果您的程序在调试模式下构建罚款,反之亦然。

This might not exactly what your problem is but good information for someone who ran into same thing as i did.

这可能不完全是您的问题,但对于遇到与我相同的事情的人来说是很好的信息。

回答by chotchki

You probably have defined all the link information in the "Debug" configuration. Which means you need to either redefine it for "Release" or move it to the "All Configurations" so its shared.

您可能已经在“调试”配置中定义了所有链接信息。这意味着您需要为“发布”重新定义它或将其移动到“所有配置”以便共享。