C++ 混合调试和发布库/二进制文件 - 不好的做法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11658915/
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
Mixing debug and release library/binary - bad practice?
提问by M W
Is it a bad practice to use a release version of 3rd party library in debug binary?
在调试二进制文件中使用 3rd 方库的发布版本是一种不好的做法吗?
I am using a 3rd party library and compiled a release .lib library. My exe is in debug mode development. Then I got:
我正在使用第 3 方库并编译了一个发行版 .lib 库。我的 exe 处于调试模式开发。然后我得到:
error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in test1.obj
After some googling I found that is because I am trying to mix release with debug, and I should probably compile the library in debug mode or otherwise muddle with the _ITERATOR_DEBUG_LEVEL macro. But I am just curious if that is the recommanded way and why. It just seem cumbersome that I need to compile and keep a record of both release and debug binaries for every 3rd party library I intend to use, which will be many very soon, while having no intention to debug into these code.
经过一番谷歌搜索后,我发现这是因为我试图将发布与调试混合在一起,我可能应该在调试模式下编译库,或者以其他方式混淆 _ITERATOR_DEBUG_LEVEL 宏。但我只是好奇这是否是推荐的方式以及为什么。我需要为我打算使用的每个 3rd 方库编译并保留发布和调试二进制文件的记录,这似乎很麻烦,这很快就会很多,而无意调试这些代码。
回答by tinman
Mixing debug and release code is bad practice. The problem is that the different versions can depend on different fundamental parts of the C++ runtime library, such as how memory is allocated, structures for things like iterators might be different, extra code could be generated to perform operations (e.g. checked iterators).
混合调试和发布代码是不好的做法。问题是不同的版本可能依赖于 C++ 运行时库的不同基本部分,例如内存如何分配,迭代器等结构可能不同,可能会生成额外的代码来执行操作(例如检查迭代器)。
It's the same as mixing library files built with any other different settings. Imagine a case where a header file contains a structure that is used by both application and library. The library is built with structure packing and alignment set to one value and the application built with another. There are no guarantees that passing the structure from the application into the library will work since they could vary in size and member positions.
它与使用任何其他不同设置构建的混合库文件相同。想象一个头文件包含应用程序和库都使用的结构的情况。该库是通过将结构打包和对齐设置为一个值而构建的,而应用程序则是用另一个值构建的。不能保证将结构从应用程序传递到库中会起作用,因为它们的大小和成员位置可能不同。
Is it possible to build your 3rd party libraries as DLLs? Assuming the interface to any functions is cleaner and does not try to pass any STL objects you will be able to mix a debug application with release DLLs without problems.
是否可以将您的 3rd 方库构建为 DLL?假设任何函数的接口更清晰并且不尝试传递任何 STL 对象,您将能够毫无问题地将调试应用程序与发布 DLL 混合使用。
回答by Luchian Grigore
The fact that it doesn't compile should be sufficient to prove it's bad practice.
它不能编译的事实应该足以证明它是不好的做法。
Regarding maintaining separate builds - you don't need to do that. Here's a workaround that previously worked for me:
关于维护单独的构建 - 您不需要这样做。这是以前对我有用的解决方法:
#ifdef _DEBUG
#define DEBUG_WAS_DEFINED
#undef _DEBUG
#endif
#include <culprit>
#ifdef DEBUG_WAS_DEFINED
#define _DEBUG
#endif
Let me know if this works for you.
让我知道这是否适合您。
回答by Andrey Chistyakov
Mixing debug and release library/binary is good and very useful practice.
混合调试和发布库/二进制文件是很好且非常有用的实践。
Debugging a large solution (100+ projects as an example) typically is not fast or even could not be possible at all (for an example when not all projects can be built in debug). Previous commentators wrote that debug/release binary could have different alignment or another staff. It's not true. All linking parameters are same in debug and release binaries because they depends on the same architecture.
调试大型解决方案(例如 100 多个项目)通常速度不快,甚至根本不可能(例如,并非所有项目都可以在调试中构建)。以前的评论员写道,调试/发布二进制文件可能有不同的对齐方式或其他人员。这不是真的。调试和发布二进制文件中的所有链接参数都相同,因为它们依赖于相同的体系结构。
You have to remove all optimizations (/Od) from the selected project. Then assign a release c++ runtime.
您必须从所选项目中删除所有优化 (/Od)。然后分配一个发布的 c++ 运行时。
The issue came because you have defined _DEBUG in the project. Remove the macro from the definitions (Project->Properties->Preprocessor->Preprocessor Definitions).
问题出现是因为您在项目中定义了 _DEBUG。从定义(项目->属性->预处理器->预处理器定义)中删除宏。
If the macro isn't in the Preprocessor Definitions, then you have to add it in "UndefinePreprocessorDefinitions".
如果宏不在预处理器定义中,则必须将其添加到“UndefinePreprocessorDefinitions”中。