底下是 Windows 上的 CodeBlocks + Pretty Printers 教學,如果是其他作業系統,請按照 CodeBlocks 官網的教學
其他方法
要看 STL 內容如果用 VC++ ,可以直接看
需要什麼
- MinGW
- Python
- CodeBlocks
步驟
安裝 MinGW 特定工具
- 下載 MinGW (官網右上角 Download Installer)
- 安裝 MinGW
- 打開 MinGW Installation Manager
- 左邊點 Basic Setup, 右邊勾選底下幾項為 Mark for Installation
- mingw32-base
- mingw32-gcc-g++
- 左邊點 All Packages -> MinGW -> MinGW Base System, 右邊勾選底下幾項為 Mark for Installation
- mingw32-gdb-python (Class 為 bin)
- 選單 Installation -> Apply Changes
- Apply
安裝 Python
- 下載 Python (2.X.X 版本,下載 32bit 版本 (Windows x86 MSI Installer) 或 64bit 版本 (Windows X86-64 MSI Installer))
Python 可以 32bit 和 64bit 都裝,詳細方法請另外搜尋,
要 debug 32bit 就用 32bit 的 Python,
要 debug 64bit 就用 64bit 的 Python
設定 Pretty Printer
- 創一個檔案 C:\MinGW\bin\pp.gdb,內容如下。
並將第三行 C:/MinGW/share/... 改為自己電腦中的資料夾 (必須用 '/' 分隔資料夾)
python
import sys
sys.path.insert(0, 'C:/MinGW/share/gcc-4.8.1/python/libstdcxx/v6')
from printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
設定 CodeBlocks
-
選單 Settings->Debugger->Default->Executable path 選
C:\MinGW\bin\gdb-python27.exe
-
選單 Settings->Debugger->Default->Debugger initialization commands 打上
source c:\MinGW\bin\pp.gdb
-
選單 Settings->Debugger->Default->Enable Watch Scripts 取消勾選
測試
- 設中斷點,用滑鼠指到變數看內容
測試用程式碼
如果你懶得自己打一個測試用的程式碼,可複製底下的測試程式碼 (C++11),並將 return 0; 設中斷點
#include <iostream>
#include <vector>
#include <map>
using namespace std;
static void print2d(vector<vector<int> > vec) {
for (auto line : vec) {
for (auto item : line) {
cout << item << " ";
}
cout << endl;
}
}
int main()
{
vector<vector<int> > foo = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
map<string, int> bar = {{"two", 2}, {"four", 4}, {"six", 6}};
return 0;
}
用滑鼠指標指 bar 時會顯示漂亮的內容,
但是你會發現指 2D 的 vector: foo 時卻不會,還是可以從 Debugger window 看到
[debug]std::vector of length 3, capacity 3 = {std::vector of length 3, capacity 3 = {1, 2, 3}, std::vector of length 3, capacity 3 = {4, 5, 6}, std::vector of length 3, capacity 3 = {7, 8, 9}}
如果是這種情況,可以自己寫一個函式,並在 debug 其間呼叫。
像是以上測試程式碼的 print2d(),在 return 0; 中斷時,在 Debugger window Command 欄輸入
call print2d(foo)
就能以自己寫的方式顯示內容
圖片
▼ 安裝 MinGW - 步驟 4
▼ 安裝 MinGW - 步驟 5
▼ 設定 CodeBlocks
▼ 沒使用 Pretty printer 看 map
▼ 有使用 Pretty printer 看 map
▼ 看 2D 的 vector
▼ 利用呼叫函式印出 2D vector 內容
FAQ
Q.1: 以上教學無效,用滑鼠指變數時沒有 Pretty print
A: 按照官網教學步驟依序操作和測試,找出原因
Q.2: 在 CodeBlocks 如何啟用 C++11
A: 選單 Settings->Compiler->Global compiler settings->Compiler settings->Compiler Flags 勾選 "Have g++ follow the C++11 ISO C++...[-std=c++11]"
Q.3: Debug 時,gdb 跳出訊息 "The program can't start because python27.dll is missing from your computer..."
A: 如果程式是 32bit,卻找不到 32bit 的 Python (C:\Windows\SysWOW64\python27.dll)
或是程式是 64bit 找不到 64bit 的 Python (C:\Windows\System32\python27.dll)
就會發生這種錯誤,解決方法是安裝相同位元數的 Python
Q.4: 我不想用 Pretty Printer,如何改用原本的 gdb?
A.1: CodeBlocks 中選單 Settings->Debugger->Default->Executable path 選
C:\MinGW\bin\gdb.exe
A.2 或是 CodeBlocks 中選單 Settings->Debugger->Default->Debugger initialization commands 將
source c:\MinGW\bin\pp.gdb
移除
Shawn http://ebola777.pixnet.net/