如果您在 C++ 上有 Makefile 项目,如何在 VScode 中 "fix" 调试器?

How to "fix" debugger in VScode if you have Makefile project on C++?

我有问题,我有用 C++ 编写的 Make 项目(多个 C++ 文件)。我正在尝试使用 VScode 调试器对其进行调试,但它只是冻结并全部删除,如何修复调试器 VSCodes 中的哪些参数 json 我必须更改 e.t.c?
项目文件夹配置:

生成文件

exe  

src(存放所有 o 和 cpp h 文件 are/will 的文件夹)
在 SRC 文件夹中:
main.cpp
WGForeCast.h
WGForeCast.cpp 等等

我的task.json

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "make",
        "args":["${workspaceFolder}/Makefile"]
    }
]
}

我的发布

{
// 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": "${workspaceFolder}/Pusk",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]
}

好吧,我找到了解决方案:
第一
在 Makefile 中,您需要向编译器添加选项 -g 标志以使用,“ -g ”:生成由基于 gdb 的调试器使用的调试信息 添加标志示例

CC=g++ -g -Wall 

以防万一在继续之前使用添加的标志重建您的项目;

其次,您需要在您的项目
中更改task.json 要创建 launch.json 文件,请在 VS Code 中打开您的项目文件夹(文件 > 打开文件夹)然后 select调试视图顶部栏上的配置齿轮图标。选择 gdb(for LInux) 然后 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": "Pusk", //I named it Pusk because i can 
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/Pusk", //path to your programs exe and exe name
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

第三,我们必须配置 task.json(基本上是使用 Makefile 而不是默认编译器启动程序的脚本) .
创造task.json

    1)Open a folder with vscode
    2)Hit F1
    3)Select "Tasks: Configure Task Runner"
    4)Hit Enter and vscode will create a sample task.json for you

task.json改成这样(可能不需要那么复杂,但是¯(ツ)/¯)

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "label": "Build",
        "type": "shell",
        "command": "make", //its like writing in console make //btw you can others commands like clean make build etc
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "problemMatcher": {
          "owner": "cpp",
          "fileLocation": ["relative", "${workspaceFolder}"],
          "pattern": {
            "regexp": "^(.*):(\d+):(\d+):\s+(warning|error):\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
          }
        }
      }
    ]
  }

Ctrl+Shift+B 重建您的项目(现在就像控制台中的 make,因为我们更改了 task.json) 全部数据!!您现在可以使用调试器了!!!
来源->see "debug in vs code" article