Out of the box Visual Studio Code is great but doesn’t do save+compile+debug very well.  The default debugging configuration just runs the debugger on existing code without saving or compiling first.

But the task system is GREAT and makes almost any possible automation pretty straightforward.  Here’s a quick example.  First set up any command line tasks you might need in your tasks.json file (Terminal > Configure Tasks > pick any task), eg:


{
    // 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",
            "group": "build"
        },
        {
            "label": "Build cmake-debug",
            "type": "shell",
            "command": "clear && bash -i -c 'at dbd'",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Build cmake-release",
            "type": "shell",
            "command": "clear && bash -i -c 'at dbr'",
            "group": "build",
            "problemMatcher": [
                "$msCompile"
            ]
        },
        {
            "label": "cmake",
            "type": "shell",
            "command": "clear && bash -i -c 'at dba'",
            "group": "build",
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "Go",
            "dependsOn": [
                "Build cmake-debug"
            ],
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}
That contains a few single-shot tasks, and another multi-task one called “Go”, where you can chain as many other tasks together as you need in the dependsOn array.  When you run Go, it will run the others in the specified order.  Here, you might note that I only needed one task in my chain (my one task does all the steps I need), but I want to future-proof by using a multi-task pattern.
From there, you can configure your … ermm.. configurations (Debug > Open Configurations).  That’s what code calls your debug task, and it’s more than a task as it figures out a lot about your compiler and debugger for you.  The configuration property we want here is called preLaunchTask, and we will specify our Go task so it runs right before the debugger starts up, eg:

{
    // 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}/cmake-debug/at_server",
            // "args": ["localhost","8000","localhost","at_viper_dev_test","b0g0","test"],
            "args": ["localhost","8000","localhost"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Go"
        }
    ]
}

Leave a Reply