VSCode 结合Makefile设置调试方法

添加构建(编译、链接等)任务(tasks.json)

ctrl+shift+p打开命令行,输入Tasks: Run task==》 Create tasks.json file from template, 生成默认的tasks.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": "echo Hello"
        }
    ]
  }

更改配置文件(launch.json)

点击左侧的Debug按钮,选择添加配置(Add configuration),然后选择C++(GDB/LLDB),将自动生成launch.json文件
默认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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

和Makefile结合

tasks.json, 3个任务build ,build-debug, clean

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "command": "make",
            "args": ["default"],
            "type": "shell"
        },
        {
            "taskName": "build-debug",
            "command": "make",
            "args": ["debug"],
            "type": "shell"
        },
        {
            "taskName": "clean",
            "command": "make",
            "args": ["clean"],
            "type": "shell"
        }
    ]
}

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": "(gdb) Launch",          //启动配置的下拉菜单中显示的名称
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/debug/sort",   // 将要进行调试的程序的路径
            "args": [],
            "stopAtEntry": false,                        // 设为true时程序将暂停在程序入口处
            "cwd": "${workspaceFolder}",        // 调试程序时的工作目录
            "environment": [],
            "externalConsole": true,                   // 调试时是否显示控制台窗口
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build-debug"                        // 调试会话开始前执行的任务,一般为编译程序
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/gbmaotai/article/details/88974988