近来学到了使用 GDB 来调试程序,了解到只有编译时让生成的可执行程序带有调试信息才能使用 GDB 去调试。于是我想起了 Visual Studio Code 的 tasks.jsonlaunch.json 文件。
我们要使用 Visual Studio Code 自带的调试面板来调试 C/C++ 程序,,可以分成两个任务,一是使用编译器生成带有调试信息可执行程序,再用 GDB 去调试。

编译

使用 gcc 命令编译并加上参数 “-g” 就可以生成所需要的调试程序,tasks.json 部分文件如下:

        {
            "label": "Build(C)(Debug)", // 调试前置的编译项目
            "windows": {
                "command": "gcc",
                "args": [  // 参数
                    "${file}",
                    "-std=c11",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}.exe",
                    "-g", // 此参数使生成的文件带有调试信息
                ],
            },
            "presentation": {
                "reveal": "always",
                "echo": false,
                "focus": true
            },
            "problemMatcher": "$gcc"
        },

调试

launch.json 文件中配置好 gdb 的命令,launch.json 文件如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C GDB Launch", // 默认调试选项名称
            "type": "cppdbg",
            "request": "launch",    // launch 为启动,attach 为附加
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "externalConsole": true,    // 打开额外的控制台窗口,设为 false 即可在集成终端中调试
            "MIMode": "gdb",    // 所使用的调试器
            "windows": {
                "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //目标程序
                "miDebuggerPath": "C:/mingw-w64/mingw64/bin/gdb.exe" // 调试程序 gdb 绝对路径
            },
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Build(C)(Debug)" // 前置任务,生成带有调试信息的可执行程序,与 tasks.json 中的任务 label 对应
        }
    ]
}

然后下断点、点击 F5 键即可开始调试,效果如图:

禁忌·多重任务

真是中二呢 给出自用的 tasks.jsonlaunch.json
tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build(C)",
            "windows": {
                "command": "gcc", // 写 C 用 gcc ,写 C++ 用 g++ , clang 同理
                "args": [ // gcc 命令后跟的参数,以 a.c 为例,这里就是:gcc a.c -std=c11 -o a.exe
                    "${file}",
                    "-std=c11", // C 标准为 c11 , C++ 为 c++17
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}.exe",
                ],
            },
            "presentation": {
                "reveal": "always",
                "echo": false, // 控制是否将执行的命令显示到面板中。笔者习惯设置成不显示
                "focus": false // 控制是否获得任务焦点。
            },
            "problemMatcher": "$gcc"
        },
        {
            "label": "Build(C++)",
            "windows": {
                "command": "g++",
                "args": [
                    "${file}",
                    "-std=c++17",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}.exe",
                ],
            },
            "presentation": {
                "reveal": "always",
                "echo": false,
                "focus": false
            },
            "problemMatcher": "$gcc"
        },
        /************************/
        {
            "label": "Build(C)(Debug)", // 调试前置的编译项目
            "windows": {
                "command": "gcc",
                "args": [
                    "${file}",
                    "-std=c11",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}.exe",
                    "-g", // 此参数使生成的文件带有调试信息
                ],
            },
            "presentation": {
                "reveal": "always",
                "echo": false,
                "focus": true
            },
            "problemMatcher": "$gcc"
        },
        {
            "label": "Build(C++)(Debug)",
            "windows": {
                "command": "g++",
                "args": [
                    "${file}",
                    "-std=c++17",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}.exe",
                    "-g",
                ],
            },
            "presentation": {
                "reveal": "always",
                "echo": false,
                "focus": true
            },
            "problemMatcher": "$gcc"
        },
        /*****************************/
        {
            "label": "\"C\" Run",
            "type": "shell",
            "dependsOn": "Build(C)",
            "windows": {
                "command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            },
            "presentation": {
                "reveal": "always",
                "echo": false,
                "focus": true
            },
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true // 使 Ctrl + Shift + B 快键键默认运行此项任务
            }
        },
        {
            "label": "\"C++\" Run",
            "type": "shell",
            "dependsOn": "Build(C++)",
            "windows": {
                "command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            },
            "presentation": {
                "reveal": "always",
                "echo": false,
                "focus": true
            },
            "problemMatcher": [],
            "group": "build" // 默认不运行此项目
        },
        {
            "label": "\"Python\" Run",
            "type": "shell",
            "windows": {
                "command": "python",
                "args": [
                    "${file}"
                ],
            },
            "presentation": {
                "reveal": "always",
                "echo": false,
                "focus": true
            },
            "problemMatcher": [],
            "group": "build" // 默认不运行此项目
        },
    ]
}

launch.json :

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C GDB Launch", // 默认调试选项
            "type": "cppdbg",
            "request": "launch",    // launch 为启动,attach 为附加
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "externalConsole": true,    // 打开额外的控制台窗口,设为 false 即可在集成终端中调试
            "MIMode": "gdb",
            "windows": {
                "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //目标程序
                "miDebuggerPath": "C:/mingw-w64/mingw64/bin/gdb.exe" // 调试程序 gdb 绝对路径
            },
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Build(C)(Debug)" // 前置任务,生成带有调试信息的可执行程序
        },
        {
            "name": "C++ GDB Launch",
            "type": "cppdbg",
            "request": "launch",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "externalConsole": true,
            "MIMode": "gdb",
            "windows": {
                "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
                "miDebuggerPath": "C:/mingw-w64/mingw64/bin/gdb.exe"
            },
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Build(C++)(Debug)"
        }
    ]
}

此时使用 终端->运行任务.. 即可在同一个目录下运行不同的任务,使得多种语言的编译运行而不用切换目录。

PS:很菜,大佬勿笑…


Leave a Reply

Your email address will not be published. Required fields are marked *