简体   繁体   中英

How to debug in VS Code: Windows 10 and MinGW

I'm trying to approaching to VS Code, I've Windows and Ubuntu on VM but i'd prefere a lot to debug on windows.

My teacher told us to use MinGW, and i'm using it for coding/compiling with no problem, i've already setted Win environment variables. I'm new to debugging world and maybe it's hard to start with VS Code, I don't know. I'm trying to follow lot of tutorial and i don't know if I'm doing it right. I must say that i'm using a Makefile instead of compiling in VS Code, i don't know if there is any difference. Here is my standard Makefile:

all: Prog.exe

Prog.exe: main.o
    g++ main.o -o Prog.exe

main.o: main.cpp
    g++ -c -g -Iinclude main.cpp

.PHONY: clean
clean:
    rm -rf *.o *.exe

1) I only know that the parameter "-g" includes debug information, is that enough for the compiling process in order to debug?

2) I'm following VS Code tutorials to set launch.json file, and i think I'm doing it right:

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "I:/Documenti/.../Prove debug/Prog.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Abilita la riformattazione per gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

I found that on VS Code site:

  • tasks.json (build instructions)
  • launch.json (debugger settings)
  • c_cpp_properties.json (compiler path and IntelliSense settings)

3) So i only need launch.json? If i don't have any .json file it asks me to choose between different Configs:

Environment Config

环境配置

4) it only seems to work the second one, but it skips breakpoints and it's useless... Can i usee "GDB" on windows? If i try to use GDB, after set all launch.json i get this error:

GDB error

GDB 错误

5) I'd prefere to debug on windows with MinGW, is it possible?

EDIT: I solved, the problems were 2 big (a, b) and 1 small (c);
a. the "program" directory;
b.the gdb/compiler directory;
c. the task.json file which is not needed if you compile with makefile (or manually with terminal).

a: i replaced "${fileDirname}\\\\${fileBasenameNoExtension}.exe" with the full directory of my program;

b: I have 2 different directory of MinGW (so 2 directory of gdb):
- One which i manually installed (in C:);
- The other I installed inside Qt (inside F:);
when VS Code automatically generate "launch.json" it sets me "miDebuggerPath" (gdb path) to the gdb installed with Qt (maybe for the order in Win10 environment paths) and I've always changed it to the other directory... Probably if I want to change directory I should set it in more files, and there was an error of 2 different gdb directory. I leaved the automatic directory (inside Qt, in F:);

c: I had an error of the task over the debug that was already working, so I realized that there were two different activities. I solved deleting the entire line "preLaunchTask": "g++.exe build active file" and deleting "task.json" file.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM