C++ 如何在发布模式下调试?

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

How to debug in release mode?

c++visual-studio-2010debuggingvisual-c++release-mode

提问by Pedro

I have to debug a c++ project, but as one dependency doesn't compile in debug mode and I haven't been able to fix that issue so far, I'd like to try to debug the project in release mode.

我必须调试一个 c++ 项目,但是由于一个依赖项不能在调试模式下编译,而且我到目前为止还无法解决这个问题,我想尝试在发布模式下调试项目。

Currently the application crashes due to a null pointer, but I haven't the code that's causing the error. As break points apparently are ignored in release-mode, I'd like to know what's the best way find the error.

目前应用程序由于空指针而崩溃,但我没有导致错误的代码。由于断点显然在发布模式下被忽略,我想知道找到错误的最佳方法是什么。

回答by Ed S.

In VS, right click your project, chose "Properties".

在 VS 中,右键单击您的项目,选择“属性”。

  1. Click the C/C++ node. Set Debug Information Format to C7 compatible (/Z7) or Program Database (/Zi).

  2. Expand Linker and click the General node. Set Enable Incremental Linking to No (/INCREMENTAL:NO).

  3. Select the Debugging node. Set Generate Debug Info to Yes (/DEBUG).

  4. Select the Optimization node. Set References to Yes (/OPT:REF).

    if /OPT:REF is specified, /OPT:ICF is on by default.

  1. 单击 C/C++ 节点。将调试信息格式设置为 C7 兼容 (/Z7) 或程序数据库 (/Zi)。

  2. 展开链接器并单击常规节点。将启用增量链接设置为否 (/INCREMENTAL:NO)。

  3. 选择调试节点。将生成调试信息设置为是 (/DEBUG)。

  4. 选择优化节点。将引用设置为是 (/OPT:REF)。

    如果指定了 /OPT:REF,默认情况下 /OPT:ICF 处于打开状态。

That's ripped directly from Microsoft's documentation:

这是直接从微软的文档中撕下来的:

I do this all of the time and pretty much never debug in debug mode anymore. As you know, many errors that occur in a release build may not occur in a debug build (almost certainly the errors that arise from invoking UB).

我一直这样做,几乎不再在调试模式下调试了。如您所知,发布版本中发生的许多错误可能不会发生在调试版本中(几乎可以肯定是调用 UB 引起的错误)。

Also, I work on a project which uses a ton of image processing and performs a lot of compression/decompression of large images. Using a slow debug build is simply impractical.

此外,我从事一个项目,该项目使用大量图像处理并对大图像执行大量压缩/解压缩。使用缓慢的调试版本是不切实际的。

回答by Yochai Timmer

You can't always just change the project settings and recompile.
Sometimes you have a released version which you would like to debug, or a dump file sent by a client.

您不能总是只更改项目设置并重新编译。
有时您有一个要调试的已发布版本,或者客户端发送的转储文件。

When compiling a C++ project in release with optimizations, the debugger sometimes doesn't show the right object information.

使用优化编译发布中的 C++ 项目时,调试器有时不会显示正确的对象信息。

The local variables are usually the first to go, and many times, the this object's information is lost to the debugger.

局部变量通常是第一个去的,很多时候,这个对象的信息会丢失给调试器。

The reason is that the compiler uses the available hardware registers to hold the information, and uses optimizations to avoid allocation of local variables.

原因是编译器使用可用的硬件寄存器来保存信息,并使用优化来避免分配局部变量。

I've suggested a way to find the missing information here:

我建议了一种在这里找到缺失信息的方法:

Debugging Release Projects in C++ - Finding the Lost Object Information

在 C++ 中调试发布项目 - 查找丢失的对象信息