|
GDB, or the GNU Debugger, is a powerful tool for debugging programs written in C and other languages. It allows you to inspect and manipulate the execution of a program, making it an essential tool for software developers.
Ensure that GDB is installed on your system. You can install it using the package manager on Linux or download it from the official GDB website.
# Example installation on Ubuntu
sudo apt-get install gdb
gcc -g -o my_program my_program.c
gdb ./my_program
break main
run
print my_variable
next
Consider the following C program (example.c
):
#include <stdio.h>
int main() {
int x = 5;
int y = 10;
int sum = x + y;
printf("Sum: %d\n", sum);
return 0;
}
1. Compile the program with debugging symbols:
gcc -g -o example example.c
2. Start GDB and set a breakpoint:
gdb ./example
break main
3. Run the program:
run
4. Print variable values and step through the code:
print x
print y
next