Home > Backend Development > C++ > How to use gdb to debug C++ programs?

How to use gdb to debug C++ programs?

WBOY
Release: 2024-06-02 09:29:57
Original
1156 people have browsed it

gdb is a tool for debugging C++ programs. Basic commands include: run: start the program break: set a breakpoint next: execute the next line of code step: execute the current function step by step print: print the expression value bt: display the stack trace Advanced features include conditional breakpoints, watchpoints, and Python scripts.

How to use gdb to debug C++ programs?

How to use gdb to debug C++ programs

Introduction

GDB (GNU Debugging debugger) is a powerful tool for debugging C++ programs. It allows developers to step through code, inspect variable values, and view stack traces. This article explains how to use gdb in C++.

Installing GDB

In most Linux distributions, gdb comes pre-installed. If you don't have it installed, you can install it using the following command:

sudo apt install gdb
Copy after login

On macOS, you can install gdb using Homebrew:

brew install gdb
Copy after login

Start GDB

To start gdb, use the following command:

gdb
Copy after login

Then you need to specify the program you want to debug. You can load a C++ program by running the following command:

(gdb) file my_program.cpp
Copy after login

Basic GDB commands

Here are some basic gdb commands for debugging C++ programs:

  • run: Start the program.
  • break: Set a breakpoint at the specified line number.
  • next: Execute the next line of code.
  • step: Execute the current function step by step.
  • print: Print the value of the expression.
  • bt: Show stack trace.

Practical case

Suppose we have a C++ program named my_program.cpp, which contains the following code:

#include <iostream>

using namespace std;

int main() {
  int a = 5;
  int b = 10;
  int c = a + b;

  cout << c << endl;

  return 0;
}
Copy after login

To debug this program, we can perform the following steps:

  1. Start gdb using the gdb command.
  2. Use file my_program.cpp to load the program.
  3. Use the run command to run the program.
  4. Use break 10 to set a breakpoint to pause the program at line 10 (here is the cout statement).
  5. Use the next command to step through the code until a breakpoint is reached.
  6. Use the print command to print the value of the variable, such as print a or print c.
  7. Use the bt command to view the stack trace.
  8. Use the continue command to continue executing the program.

Advanced features

gdb also provides many advanced features, such as:

  • Conditional breakpoints:Trigger breakpoints only when specific conditions are met.
  • Observation point: The event is triggered when the value of the variable changes.
  • Python script: Allows automated debugging tasks.

Conclusion

gdb is a powerful tool that can be used to debug C++ programs. By mastering basic commands and advanced features, developers can effectively find and fix errors in their code.

The above is the detailed content of How to use gdb to debug C++ programs?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template