How to use the ggb MinGW debugger to debug a C ++ program on Windows?

I was looking for documentation on this and found nothing. I have MinGW installed and it works great. I just don't know how to use the debugger.

Given some simple code, let's say in a file called "mycode.cpp":

int main() { int temp = 0; for (int i = 0; i < 5; ++i) temp += i; return 0; } 

... how to debug this. What commands do I use to debug code using MinGW and GDB in windows? Can I execute the code through the command line, as in Visual Studio? If so, what commands do I use for this?

Do you have any tutorials on using GDB? I could not find anyone, but if someone could direct me to what would be great. I'm tired of writing many std::cout statements to debug complex code.

+25
c ++ debugging windows mingw gdb
Jan 12 '11 at 17:30
source share
2 answers

The first step is to compile your program with -g to include debugging information in the executable:

 g++ -g -o myprog.exe mycode.cpp 

Then the program can be downloaded to gdb :

 gdb myprog.exe 

A few commands to run:

  • break main will interrupt debugging when calling main . You can also break lines of code with break FILENAME:LINENO . For example, break mycode.cpp:4 interrupts execution whenever the program reaches line 4 of mycode.cpp .
  • start starts the program. In your case, you need to set breakpoints before starting the program, because it ends quickly.

At the breakpoint:

  • print VARNAME . This is how you print the values โ€‹โ€‹of variables, whether local, static, or global. For example, in a for loop, you can enter print temp to print the value of the temp variable.
  • step This is equivalent to "step into".
  • next or adv +1 Go to the next line (for example, "step over"). You can also go to a specific line in a specific file, for example, adv mycode.cpp:8 .
  • bt Print the return line. This is a stack trace, essentially.
  • continue Just like the "continue" operation of a visual debugger. This causes the program to continue to the next breakpoint or exit point.

It is best to read the GDB User Guide .

+39
Jan 12 '11 at 17:40
source share

There are several guis gdb for windows in this question Windows version of DDD interface DDD

Although DDD was not carried forward

+5
Jan 12 '11 at 18:33
source share



All Articles