vscode调试C/C++报错:the program has exited with code 42 (0x0000002a).

记录一下解决使用VScode写C/C++编译代码出现报错的问题。

报错类型

我的VScode事先已经配置好相关环境了,安装的是mingw64。点击调试后出现如下报错
终端提示:

正在启动生成...
C:mingw64bincpp.exe -fdiagnostics-color=always -g C:gyqC++test.cpp -o C:gyqC++test.exe
生成已成功完成。
 *  终端将被任务重用,按任意键关闭。

弹出窗口:

Unable to start debugging. Program path '***.exe' is missing or invalid.
GDB failed with message: "***.exe": not inexecutable format: File format not recognized
This may occur if the process's executable was changed after the process was started, such as when installing an update. Try re-launching the application or restarting the machine.

解决方法

修改launch.json和tasks.json中所有的cpp.exe为g++.exe。提供参考
launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\mingw64\bin\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\mingw64\bin\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

建议

通过我自己解决的过程中查阅的相关信息,如果通过上述手段还是不可以的话,提供几点建议
1、检查是否修改上面两个文件中路径为自己电脑路径
2、检查mingw64bin中是否存在g++.exe文档