How do I use the MinGW gdb debugger to debug a C++ program in Windows?
The first step is to compile your program with -g
to include debugging information within the executable:
g++ -g -o myprog.exe mycode.cpp
Then the program can be loaded into gdb
:
gdb myprog.exe
A few commands to get you started:
break main
will cause the debugger to break whenmain
is called. You can also break on lines of code withbreak FILENAME:LINENO
. For example,break mycode.cpp:4
breaks execution whenever the program reaches line 4 ofmycode.cpp
.start
starts the program. In your case, you need to set breakpoints before starting the program because it exits quickly.
At a breakpoint:
print VARNAME
. That's how you print values of variables, whether local, static, or global. For example, at thefor
loop, you can typeprint temp
to print out the value of thetemp
variable.step
This is equivalent to "step into".next
oradv +1
Advance to the next line (like "step over"). You can also advance to a specific line of a specific file with, for example,adv mycode.cpp:8
.bt
Print a backtrace. This is a stack trace, essentially.continue
Exactly like a "continue" operation of a visual debugger. It causes the program execution to continue until the next break point or the program exits.
The best thing to read is the GDB users' manual.
How to debug in VS Code: Windows 10 and MinGW
1) Yes. the -g
switch is all you need. Just to be clear I'm compiling my program as
gcc -g prog.c
2) Set the "externalConsole"
options as "true". For some reason, this does not work when it is false. The rest seems okay.
3) I can debug just fine with only the launch.json
file and nothing else
4) The second one C++ (Windows)
option is for the Windows C/C++ compiler which you can install using MSBuild tools alongside Visual Studio and use the cl.exe
compiler instead of gcc/g++. In case you want to try the Windows compiler, you need to compile using the -Zi switch (just like -g switch in gcc/g++) and your compilation command will be
cl -Zi path\to\prog.cpp -o a.exe
But of course, if you are using MinGW/gcc/g++ you don't need this.
I can't say for sure, but your error looks like your path to the program is not correct. Make appropriate changes in the "program"
in your launch.json file just for reference my program
path value is
"program": "${workspaceFolder}/a.exe",
Here, this a.exe
is located at the root of the repository/workspace of VS Code
5) Well, I'm able to debug in windows with MinGW just fine, so it should be possible.
How to use MSYS2/MINGW64 gdb to debug a program built with MSYS2/MINGW64 toolchain but not built from inside the MSYS2 shell
GNU has cross-platform toolchains. You can use those to (kind of) build and debug programs for other platforms. For simplicity, we'll concern ourselves only with gcc and gdb.
Both gcc and gdb has cross platform versions, so you can be on Linux/Windows and compile C programs for Windows/Linux. These GNU tools have compilation arguments called host
and target
. So you can compile gcc/gdb to run on Windows system and compile/debug programs for Linux system.
The mingw64 gdb has both target
and host
set up to be x86_64-w64-mingw32. So it runs on Windows system and debugs programs compiled by Mingw-w64 toolchain.
The Msys2 gdb, on the other hand, has target and host set up to be x86_64-pc-msys. So it'll only run on msys systems, which means you'll have to invoke it from inside the Msys2 shell (Otherwise it won't find the cygwin.dll
and won't perform properly, if it does at all). Moreover, it can only debug programs compiled for msys systems.
In short, to debug programs compiled by Mingw-w64 toolchain, use the mingw64 gdb.
Debugging MinGW program with gdb on Windows, not terminating at assert failure
Just set a breakpoint on exit:
(gdb) b exit
Visual studio code - debbugging stopped working [gdb, mingw, windows 10]
The accepeted answer was given by olyBlackCat in the comments, which I'm quoting here to mark it as the accepted answer:
I'm seeing a lot of questions like this. Maybe try this extension instead of the default C++ one? It works for me on Linux. – HolyBlackCat Aug 16 at 17:28
Found the bug report. There's some advice there github.com/microsoft/vscode-cpptools/issues/7971
I am using MinGw compiler, whenever I'm trying to debug the code gets error failed. And why its location is of cygwin as I am using mingw
You will need GDB to debug code built with MinGW's gcc.
Here's a link explaining how it can be done: https://code.visualstudio.com/docs/cpp/cpp-debug
Also it looks like you're using an old version (GDB 8.1 while the current version is 9.2). Check out http://winlibs.com/ for a build of the latest version of MinGW-w64 which includes GDB 9.2.
Can I use a Code::Blocks as a MinGW GDB GUI?
As a GUI tool for the debugger you could use x64dbg.
It allows you to perform both 32 and 64 bit debugging for programs built with either MinGW or Visual Studio
Related Topics
When to Use Shared_Ptr and When to Use Raw Pointers
How to See the Template Instantiated Code by C++ Compiler
Using Boost Thread and a Non-Static Class Function
Practical Uses for the "Curiously Recurring Template Pattern"
Cmake - Remove a Compile Flag for a Single Translation Unit
Convert Float to Std::String in C++
Translating Python Dictionary to C++
What Information Does Gcc Profile Guided Optimization (Pgo) Collect and Which Optimizations Use It
Fastest Algorithm for Primality Test
Conflict Between Dynamic Linking Priority in Osx
Treating Memory Returned by Operator New(Sizeof(T) * N) as an Array
Why Is #Define Bad and What Is the Proper Substitute
Can't C++ Pod Type Have Any Constructor
Constructor Invocation Mechanism
Integer Division Rounding with Negatives in C++