Debug c++ in cmd windows 10

I've never used a debugger and the time has come to give them a try. MinGW appears to come with GDB which I've been trying to use. Supposdly running gdb from the command line and typing run myprog.exe starts the debugger but when I do this I get

Starting program: C:\MinGW\bin\myprog.exe MyProg.exe [New Thread 1828.0xd8c] Error opening file. [Inferior 1 (process 1828) exited with code 02]

How to proceed or what's an easier way?

In particular I'm trying to flush out undefined behavior.

asked Oct 4, 2013 at 8:36

Celeritas's user avatar


14.2k35 gold badges107 silver badges188 bronze badges

Since your program terminates, you'll need to set a breakpoint to see anything. Try break main before the run line. Then you can do commands line next (next line), step (step into/outof function calls), print expression (where expression can be a variable name or a function-call or a calculation), display expression (same as print, but prints just before each prompt). At any given point you can type backtrace to get a call stack. You can even type up and down to move up the callstack, so you can print higher local variables.

answered Oct 4, 2013 at 8:48

luser droog's user avatar

luser droogluser droog

18.7k3 gold badges51 silver badges102 bronze badges


Well, the easiest way would be to use an IDE, actually. You might want to give code::blocks a try - very easy to use, configures everything for you on installation (just make sure to pick a compiler - don't worry, it'll prompt you) and there, you're all set and ready to go. As it's multi-platform, it doesn't really lock you into windows either, and gives you very powerful (and, I guess more importantly, convenient) possibilities of graphical debugging.

answered Oct 4, 2013 at 8:39

Fenixp's user avatar


6455 silver badges22 bronze badges


pass the binary with gdb

gdb <binary>

then set breakpoint to main

gdb) break main

Then run your program in gdb

gdb) run

then break point hits use 'n' or 'next' to step to different lines

gdb) n

Use 's' for stepping into function and 'p' printing var value

Example :

gdb) s <fun_name> 
gdb) p x 

answered Oct 4, 2013 at 9:08

Sohil Omer's user avatar

Sohil OmerSohil Omer

1,1717 silver badges14 bronze badges

I would suggest , as a beginner start off with Visual Studio. It has a very good and easy to use debugger. Just create a break point in the line from which you want to start debugging (click on the left bar beside the line or right click and create a break point). Once your break points are set you can just simply run the program in debug mode and the execution of the program will halt in the point where the break was created.

At this point you should be able to view all valuable information about the execution of the program. You can use F10 to continue the execution step or F11 to step inside the execution tree.

The debugger as many other advanced features like break on condition , hit count etc but you can start off with it's basic functionality.

answered Oct 4, 2013 at 10:33

Genocide_Hoax's user avatar


8332 gold badges18 silver badges41 bronze badges


If I compiled a program like this:

gcc -o my-prog -g myprog.c

I could then debug the executable my-prog it like this:

gdb my-prog

The -g option tells gcc to generate full debugging info. Other compilers will have their own versions of this option (e.g. the MSVC cl command has the /Zi option).

Since you're having issues running the gdb on your program, it might be worth checking if it was compiled with debugging info in the first place. The debugging info is usually generated in the same location as where you compiled your program.

answered Sep 29, 2021 at 15:38

angstyloop's user avatar

How to debug in cmd Windows 10?

Debug syntax After debug starts, type "?" to display a list of debugging commands. To get out of debugging mode, you need to type "Q" and press Enter . To execute the debug routine, you need to type "G" and press Enter .

How to debug through cmd?

To activate the debugger at the command prompt.
Choose Debug Next. The debugger is now active and is waiting to attach to a session..
Select a session, and then choose Debug. The debugger is now active and attached to the selected session..

How to debug C code in Windows?

Debugging C programs.
To compile the program, use: gcc -g -Wall *. c . ... .
Now enter gdb with the command gdb a. out ..
At the gdb prompt, enter run . ... .
You can enter where , and gdb will show the current stack of subroutine calls, and the line number for each. ... .
Type quit to exit gdb and return to the shell prompt..

How to debug in terminal C?

How to Debug C Program using gdb in 6 Simple Steps.
Write a sample C program with errors for debugging purpose. ... .
Compile the C program with debugging option -g. ... .
Launch gdb. ... .
Set up a break point inside C program. ... .
Execute the C program in gdb debugger. ... .
Printing the variable values inside gdb debugger..