如何使QT的Release模式可调试(Windows)

版权声明:本文为itas109原创文章,未经允许不得转载引用或用于商业用途。【http://blog.csdn.net/itas109】【[email protected]】 https://blog.csdn.net/itas109/article/details/83652387

如何使QT的Release模式可调试(Windows)


如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033

目录

环境:
QT版本:5.6.2
开发环境:VS2013
系统版本:windows 7 64bit


前言

我们很多情况下是需要在Release模式下调试QT程序的,如联合dmp文件输出崩溃信息辅助定位问题、引用第三方库没有Debug版本但是有需要单步调试程序等等。
本文将介绍QT如何在Release模式下调试程序。

1.PDB简介

PDB(Program Database,程序数据库)文件保存调试和项目状态信息,允许增量链接程序的Debug配置。使用/ ZI或/ Zi(用于C / C ++)构建时,将创建PDB文件。
每次创建OBJ文件时,C / C ++编译器都会将调试信息合并到VCx0.PDB中。插入的信息包括类型信息,但不包括诸如功能定义之类的符号信息。因此,即使每个源文件都包含公共头文件(如<windows.h>),这些头文件中的typedef也只存储一次,而不是存储在每个OBJ文件中。
链接器创建project.PDB,其中包含项目的EXE文件的调试信息。 project.PDB文件包含完整的调试信息,包括函数原型,而不仅仅是VCx0.PDB中的类型信息。两个PDB文件都允许增量更新。链接器还将.pdb文件的路径嵌入到它创建的.exe或.dll文件中。

MSDN原文:
A program database (PDB) file holds debugging and project state information that allows incremental linking of a Debug configuration of your program. A PDB file is created when you build with /ZI or /Zi (for C/C++).
In Visual C++, the /Fd option names the PDB file created by the compiler. When you create a project in Visual Studio using wizards, the /Fd option is set to create a PDB named project.PDB.
If you build your C/C++ application using a makefile, and you specify /ZI or /Zi without /Fd, you end up with two PDB files:
VCx0.PDB, where x represents the version of Visual C++, for example VC100.PDB. This file stores all debugging information for the individual OBJ files and resides in the same directory as the project makefile.
project.PDB This file stores all debug information for the.exe file. For C/C++, it resides in the \debug subdirectory.
Each time it creates an OBJ file, the C/C++ compiler merges debug information into VCx0.PDB. The inserted information includes type information but does not include symbol information such as function definitions. So, even if every source file includes common header files such as <windows.h>, the typedefs from those headers are stored only once, rather than being in every OBJ file.
The linker creates project.PDB, which contains debug information for the project’s EXE file. The project.PDB file contains full debug information, including function prototypes, not just the type information found in VCx0.PDB. Both PDB files allow incremental updates. The linker also embeds the path to the .pdb file in the .exe or .dll file that it creates.
The Visual Studio debugger uses the path to the PDB in the EXE or DLL file to find the project.PDB file. If the debugger cannot find the PDB file at that location or if the path is invalid (for example, if the project was moved to another computer), the debugger searches the path containing the EXE, the symbol paths specified in the Options dialog box (Debugging folder, Symbols node). If the debugger cannot find a .PDB file, a Find Symbols dialog box appears, which allows you to search for symbols or to add additional locations to the search path.

2./Z7、/Zi、/ZI(调试信息格式)

语法

/Z{7|i|I}  

下表描述了这些选项。

2.1 无

不生成任何调试信息,因此编译较快。

2.2 /Z7

生成包含用于调试器的完整符号调试信息的 .obj 文件。 符号化调试信息包含变量的名称和类型以及函数和行号。 不生成任何 .pdb 文件。
对于第三方库的分发服务器,不生成 .pdb 文件是一个优点。 但是,在链接阶段和调试期间,用于预编译头的 .obj 文件是必需的。 如果 .pch 对象文件中只有类型信息(没有代码),则还必须使用 /Yl(为调试库插入 PCH 引用) 进行编译。

2.3 /Zi

生成一个程序数据库(PDB),其中包含供调试器使用的类型信息和符号化调试信息。 符号化调试信息包含变量的名称和类型以及函数和行号。
/Zi 不影响优化。 但是,/Zi 的确暗示了 /debug;有关更多信息,请参见 /DEBUG(生成调试信息)。
类型信息放置在 .pdb 文件而不是 .obj 文件中。
可以将 /Gm(启用最小重新生成) 和 /Zi 结合使用,但使用 /Z7 编译时不能使用 /Gm。
使用 /Zi 和 /clr 编译时,DebuggableAttribute 特性将不会放置到程序集元数据中;如果要使用该特性,则必须在源代码中指定它。 该特性可影响应用程序的运行时性能。 有关 Debuggable 特性如何影响性能以及如何减轻性能影响的更多信息,请参见令映像更易于调试。

2.4 /ZI

采用支持“编辑并继续”功能的格式生成程序数据库(如上所述)。 如果想使用“编辑并继续”调试,则必须使用此选项。 因为大多数优化与“编辑并继续”不兼容,所以使用 /ZI 会禁用代码中的所有 #pragma optimize 语句。
/ZI 会导致在编译中使用 /Gy(启用函数级链接) 和 /FC(所诊断源代码文件的完整路径)。
/ZI 与 /clr(公共语言运行时编译) 不兼容。

说明:
/ZI 仅可在面向 x86 的编译器中使用;此编译器选项不能在面向 x64 或 ARM 处理器的编译器中使用。

3.QT中设置Release模式可调试

3.1 全局设置

目录:Qt5.6.2\5.6\msvc2013\mkspecs\common
文件:msvc-desktop.conf

QMAKE_CFLAGS_RELEASE     = -O2 -MD -Zi
QMAKE_LFLAGS_RELEASE     = /INCREMENTAL:NO /DEBUG

3.2 项目设置

在pro文件中加入

QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
QMAKE_LFLAGS_RELEASE = $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO

注意:
如果项目中有多个pro文件,如一个主程序+多个lib的方式,需要给每一个需要调试的pro都加入上面的配置。

4.结果

重新启动QT Creator获取重新编译后,会发现
a.程序产生了*.pdb文件
b.release版本可以和Debug一样调试了


Reference:

  1. MSDN : Program Database Files (C++)
  2. MSDN : /Z7、/Zi、/ZI(调试信息格式)

觉得文章对你有帮助,可以用微信扫描二维码捐赠给博主,谢谢!
微信
如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033

猜你喜欢

转载自blog.csdn.net/itas109/article/details/83652387