Build before Debug in VSCode

For debugging Blender I have set up my IDE Visual Studio Code such that it builds Blender whenever I start the debugger. Before I did this, I would ofte forget to manually build Blender, causing all kinds of confusion when the code didn’t match up with the behaviour.

Running a compiler before starting the debug sessions concerns a few files. First we need a task in .vscode/tasks.json. Here I added two tasks, one for a release-mode build and one for a debug-mode build:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Release",
            "type": "shell",
            "command": "nice -n 20 ninja -C /home/sybren/workspace/blender-git/build_linux/",
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "Build Debug",
            "type": "shell",
            "command": "nice -n 20 ninja -C /home/sybren/workspace/blender-git/build_debug/",
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

Then we need to update launch.json to run that task before starting Blender itself. Note the preLaunchTask, which is set to the label of the task to run:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Blender",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/sybren/workspace/blender-git/build_debug/bin/blender",
            "args": [],
            "stopAtEntry": false,
            "cwd": "/home/sybren/workspace/blender-git/blender",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "Build Debug",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

That’s all there is to it. Now whenever I press F5, a new debug build is made, and Blender starts, and debugging can begin.

dr. Sybren A. Stüvel
dr. Sybren A. Stüvel
Open Source software developer, photographer, drummer

Related