无法在 Visual Studio 代码中为 C++ 启动调试器

sha*_*end 3 c++ json visual-studio-code

我只是从 Netbeans 切换到 Visual Studio 代码,但无法调试 c++,错误是Unable to start debugging. Launch options string provided by the project system is invalid. Unable to determine path.... 我试图遵循我从谷歌搜索的 Visual Studio 代码网站上的 c/c++ 调试指南,但它无法运行该应用程序,但我可以从中编译 c++,Ctrl + Shift + B所以我的 task.json 文件是正确的,所以这是我的任务。 json 文件和 launch.json 文件。

{
    //Task.json
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-pipe", "-std=c++14", "${fileBasename}", "-lm"],
    "showOutput": "always"
}
Run Code Online (Sandbox Code Playgroud)
//Launch.json
"version": "0.2.0",
"configurations": [
    {
        "name": "C++ Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceRoot}/a.out",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": true,
        "linux": {
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        "osx": {
            "MIMode": "lldb"
        },
        "windows": {
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    },
Run Code Online (Sandbox Code Playgroud)

RLa*_*aaa 7

我只是通过执行以下操作来修复它(在 Windows 下)

  1. https://sourceforge.net/projects/tdm-gcc/?source=typ_redirect安装的TDM-GCC MinGW 编译器 使用默认安装路径选项。

  2. 将这些文件夹添加到 PATH 环境变量

    C:\TDM-GCC-64\bin

    C:\TDM-GCC-64\gdb64\bin

  3. 在你的launch.json中添加gdb调试器的路径,你的windows部分应该像下面的代码

     "windows": {
                    "MIMode": "gdb",
                    "miDebuggerPath": "C:/TDM-GCC-64/gdb64/bin/gdb.exe",
                    "setupCommands": [
                        {
                            "description": "Enable pretty-printing for gdb",
                            "text": "-enable-pretty-printing",
                            "ignoreFailures": true
                        }
                    ]
                }
    Run Code Online (Sandbox Code Playgroud)

  4. 重新启动 Visual Studio Code 并尝试编译 ( ctrl+shift+b) 并运行调试器 ( f5)

备注:就像其他人提到的那样,您应该在 tasks.json args 中添加 -g 选项,以便使用调试信息构建可执行文件。