Vscode debugging environment configuration method to solve The preLaunchTask'C/C++: gcc.exe build active file'

I want to use Vscode to debug the code. I checked a lot of environment configuration information on the Internet, but I don’t know why this problem always occurs: After
Insert image description here
researching for a long time, I finally found a violent solution to this problem. I will record it here.

1. Generate tasks.json file

Shortcut key Ctrl+Shift+P, find it Tasks: Configuration Default Build Task, the following window will pop up:
Insert image description here
Here select the location where gcc is used on the computer, here is C:/MinGW/bin/gcc.exe, and
then the tasks.json file will be generated
on my computer. Each parameter of the generated file above is as follows:
Insert image description here
After testing, it was found that "type" must be modified to "shell". I did not delve into why it was modified in this way. At least under "cppbuild"this parameter, debugging will cause the error shown at the beginning.

2. Generate launch.json

The process of generating launch.json is also very strange.
First, select Add Configuration in Run, and select C++ (GDB/LLDB) in the pop-up window.
Insert image description here
Launch.json will indeed be generated at this time, but the file is empty and must be repeated again. In the just operation, after selecting C++ (GDB/LLDB), a new window will pop up. Insert image description here
In this window, select C/C++: (gdb) Launch.
The launch.json generated at this time is like this:
Insert image description here
Next, you need to ”program“modify it to ${fileDirname}/${fileBasenameNoExtension}.exe, and then "miDebuggerPath"Corresponding to the path to gdb, finally add a line at the end: "preLaunchTask": "C/C++: gcc.exe build active file", preLaunchTaskthe content here needs to labelbe consistent with the content in tasks.json.
After the modification is completed, it will look like this:
Insert image description here

After completing the above tasks, you can successfully debug C code.

Guess you like

Origin blog.csdn.net/qq_38604345/article/details/126898103
Recommended