問題描述
在調試二進制文件中使用第 3 方庫的發布版本是一種不好的做法嗎?
Is it a bad practice to use a release version of 3rd party library in debug binary?
我正在使用第 3 方庫并編譯了一個發行版 .lib 庫.我的 exe 處于調試模式開發.然后我得到:
I am using a 3rd party library and compiled a release .lib library. My exe is in debug mode development. Then I got:
error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in test1.obj
經過一些谷歌搜索后,我發現這是因為我試圖將發布與調試混合在一起,我可能應該在調試模式下編譯庫,或者以其他方式混淆 _ITERATOR_DEBUG_LEVEL 宏.但我只是好奇這是否是推薦的方式以及為什么.我需要為我打算使用的每個 3rd 方庫編譯并保留發布和調試二進制文件的記錄,這似乎很麻煩,這很快就會很多,而無意調試這些代碼.
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.
推薦答案
混合調試和發布代碼是不好的做法.問題是不同的版本可能依賴于 C++ 運行時庫的不同基本部分,例如如何分配內存,諸如迭代器之類的結構可能不同,可能會生成額外的代碼來執行操作(例如檢查迭代器).
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).
這與使用任何其他不同設置構建的混合庫文件相同.想象一個頭文件包含應用程序和庫都使用的結構的情況.該庫是通過將結構打包和對齊設置為一個值而構建的,而應用程序則是用另一個值構建的.無法保證將結構從應用程序傳遞到庫中會起作用,因為它們的大小和成員位置可能不同.
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.
是否可以將您的第 3 方庫構建為 DLL?假設任何函數的接口更清晰并且不嘗試傳遞任何 STL 對象,您將能夠毫無問題地將調試應用程序與發布 DLL 混合使用.
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.
這篇關于混合調試和發布庫/二進制文件 - 不好的做法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!