21. August 2020

Common mistake why GDB debugger does not work in Visual Studio Code

Visual Studio Code has great C/C++ support and it’s possible to debug a project with it.

Visual Studio Code can use gdb as a debugger.

If you have your project with a binary and you press F5 to launch the debugger, then Visual Studio will prompt you to configure GDB launcher in launch.json.

Here is sample launch.json for binary my_application on Linux:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/my_application",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }  
    ]
}

Just define a path to your binary, save the file and press F5.

Still, it might happen that debugger does not stop at your break point. Why?

There is one very common mistake. People often forget to add -g for gcc parameter when building the project.

GCC without parameter -g will build a release version of the project without debug information. To fix the problem just add -g to your Makefile, clean, and rebuild the project.