vscode配置调试C/C++程序——linux环境,命令行编译

虽然linux环境下使用命令行编译可以使用gdb调试,但是不能跟随代码一步一步走,很麻烦

但是vscode通过配置task.json和launch.json可以达到一步一跟的效果。

对于文件不多的项目可以使用vscode模拟命令行编译效果来调试

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 hello world",     // task的名字
            "type": "shell",   
            "command": "gcc",    //编译命令
            "args": [    //编译参数列表
                "-g", // 加上-g可以断点调试
                "2048.c",
                "-o",
                "2048",
                "-lcurses"
            ]
        }
    ]
}

其中args里面的参数就是你使用命令行模式里的参数,对照着网上抄就行。

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": "debug hello world",    //名称
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/2048",    //当前目录下编译后的可执行文件
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",    //表示当前目录
            "environment": [],
            "externalConsole": false, // 在vscode自带的终端中运行,不打开外部终端
            "MIMode": "gdb",    //用gdb来debug
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build hello world"  //在执行debug hello world前,先执行build hello world这个task,看第4节
        }
    ]
}

将 "program": "${workspaceFolder}/2048", 中的2018修改为task.json中你所生成的程序名。

应该有相关变量来代替这两步的修改,可以适配所有的编译调试命令。等我有时间去看一下官方文档来操作一下。

现在就目前将就着用一下。