Use Emacs GDB to debug c/c++ programs.

Prepare: Compile

Compile c/cpp with option -g:

gcc prog.c -g -o proc

or in Makefile:

CFLAGS=“-g” ./configure && make

Start: Run gdb

Run gdb

M-x gdb to select a file.

Set mode

gdb-mamy-windows to switch single/multi windows mode.

gdb-restore-windows to restore windows.

Debug: Set breakpoint

Set/Remove breakpoint

C-x C-a C-b or gud-break to set a breakpoint.

C-x C-a C-d or gud-remove to remove a breakpoint.

Run

gud-go to run, and stop on the breakpoint.

Next/Step

C-x C-a C-n or gud-next to run a function.

C-x C-a C-s or gud-step to run into a function.

C-x C-a C-f or gud-finish to finish a function.

C-x C-a C-u or gud-until to run until the cursor line.

C-x C-a C-r or gud-cont to continue to run.

View: Watch variables

In the default locals buffer

If not shown, gdb-display-locals-buffer to toggle.

In the speedbar buffer

C-x C-a C-w or gud-watch to add a variables.

On mouse over

If not shown, gud-tooltip-mode to toggle.

IO: Set IO or redirect

Default IO buffer

gdb-use-separate-io-buffer to toggle.

Redirect to files

In gdb buffer type: *run < file.in > file.out