xavier-cai / vscode-leetcode-cpp-debug

Debug support for LeetCode with C++
https://marketplace.visualstudio.com/items?itemName=XavierCai.vscode-leetcode-cpp-debug
MIT License
25 stars 5 forks source link

Failed to start debugging: Error: Configuration '(gdb) Launch' is missing in 'launch.json'. #15

Closed alexya closed 3 years ago

alexya commented 3 years ago

我按照插件的提示,一步一步安装。但最后运行时,报错提示如上。 我估计是launch里面的配置不对,你能够分享一个你的配置我看一下么?

比如,我不知道.exe应该替换成什么,以及miDebuggerPath该 填写什么内容?

    "launch": {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "(gdb) Launch",
                "type": "cppdbg",
                "request": "launch",
                "program": "${workspaceFolder}/leetcode-main.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "miDebuggerPath": "/path/to/gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }

g++ 和 gdb应该都安装好了。命令行里面运行,能够显示正确的版本。

g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gdb --version
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

Windows 10 VSCode 都是最新版

image

xavier-cai commented 3 years ago

先回答一下你直接提出的问题,以下是我的launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ debug Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/ProgramData/MinGW/bin/gdb.exe",
            "preLaunchTask": "g++",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

其中"miDebuggerPath": "D:/ProgramData/MinGW/bin/gdb.exe",是我的gdb路径,"preLaunchTask": "g++"是我在tasks.json里定义的编译任务,此处一并提供:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "-std=c++14",
                "-Wall"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
xavier-cai commented 3 years ago

再说一下我觉得的问题原因和建议:

看起来直接的原因更像是C++调试环境没有配置好,不知道你那边是否可以正常调试其他带main函数的C++程序,如果不行或者没有试过,那么我建议可以先使用vscode的调试功能去调试一个hello world程序,按引导来自动生成launch文件,无需手动建立。

更详细的信息可以参考官方的C++调试文档:https://code.visualstudio.com/docs/cpp/config-mingw#cpp-atricles

alexya commented 3 years ago

谢谢。我找到一个workaround

我选择生成的leetcode-main.cpp等临时文件不删除,然后添加一个task。 在debug之前,先选择这个task,编译出来leetcode-main

    {
      "label": "build leetcode-main",
      "type": "shell",
      "command": "g++",
      "args": ["-g", "-o", "leetcode-main", "leetcode-main.cpp"],
      "group": {
        "kind": "build",
        "isDefault": true // press Ctrl Shift B to run the task directly when it is true
      }
    }

我不知道为什么这个插件不能全自动。我再按你上面的config试一下。

updated: 知道了,你有一个preLaunchTask,就是运行了一个task去生成编译文件。

注:建议在插件里面说一下,像我这样,以前完全没有用过vscode 编译和debug 过c++,这个是真的没有想到。我“原以为”,插件会生成临时文件,自动编译再attach到debugger上面......

xavier-cai commented 3 years ago

实际上,readme里面有提到 注意: 在使用此插件启动调试前, 你必须确定你有可用的C++调试工具. 从官方文档可以获取更多相关信息.。 大概改成你必须确定你已经能在VSCode中顺利启动C++调试会好一点吧。

alexya commented 3 years ago

目前,保留生成的leetcode-main.cpp,在该文件上面,运行launch.json里面定义的 比如“C++ debug Launch”。会提示我找不到 task (g++就是按照你分享给我的配置的)。launch.json和 task.json里面的名字是匹配的,为什么还是找不到。

比如:

image

google了一下,貌似是workspace的问题,所以, 我的launch是保存在user settings里面,文件路径在C:\Users\\AppData\Roaming\Code\User\settings.json 我的task.json在项目目录下,C:\src\github\lc.vscode\tasks.json

请教一下,你的这两个文件,对应的路径是?

谢谢

xavier-cai commented 3 years ago

先回答你的问题:这两个文件都在我workspace根目录下的.vscode文件夹下。 我仍然建议在workspace下建一个hello world程序然后去使用vscode的Run and Debug功能,会通过引导程序来帮助你建立配置文件,不建议手动去建立这些配置文件。