1. 程式人生 > >如何使QT的Release模式可除錯(Windows)

如何使QT的Release模式可除錯(Windows)

如何使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