Debug using GNU Debugger (GDB)
The GNU Debugger (GDB) is a powerful tool that allows software developers to see what is happening inside a program while it runs or what it was doing at the moment it crashed. This guide provides an overview of the basic commands needed to start using GDB for debugging C and C++ programs.
Starting GDB
To start GDB, use the following command:
gdb <your_program>Setting Breakpoints
Breakpoints allow you to pause the execution of your program at specific points. To set a breakpoint, use:
break <function_name>or
break <file_name>:<line_number>Running the Program
To run the program within GDB, use:
runStepping Through Code
To step through your code line by line, use:
stepor to step over function calls, use:
nextInspecting Variables
To inspect the value of a variable, use:
print <variable_name>Continuing
To continue running the program until the next breakpoint, use:
continueExiting
To exit GDB, use:
quitConclusion
GDB is an essential tool for debugging C and C++ programs. By mastering its basic commands, you can significantly improve your debugging efficiency.