Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple GDB C++ debugging on VS Code fails with a NullReferenceException

My source file.cpp:

#include <iostream>
using namespace std;
int main()
{
    int x = 1;
    int y = 2;
    cout << x + y << endl;
    return 0;
}

I compile it using g++ -g file.cpp, and then my launch.json file is like so:

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

My directory structure is:

- workspaceFolder
  - .vscode
    - launch.json
  - a.out
  - file.cpp

When I click the green "Start debugging" arrow in the Debug tab I get this error in the Debug Console:

Stopping due to fatal error: NullReferenceException: Object reference not set to an instance of an object

What am I doing wrong? I have looked at every tutorial and debugging C++ seems to be working for all of them except in my case. I also have the C/C++ (Microsoft) extension installed, and am running Ubuntu 64-bit.

like image 923
Gaurang Tandon Avatar asked Dec 25 '18 10:12

Gaurang Tandon


People also ask

Why is my VS Code debugger not working?

Debugger not working Look at the debugger console for any error messages displayed. Look at the Debugger Tools console output for any errors. Remember to re-start VS Code once done (this won't be necessary in a future release). Solution: Clear all expressions from the debugger Watch window and start debugging again.

How do I run a GDB code in Visual Studio?

Example launch. Click and select C/C++ : (gdb) Attach option. To start debugging with VSCode, select the process name for your vehicle's binary : example arducopter . Before or after attaching you may put breakpoints in the code to start debugging.

Does VS Code use GDB?

Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB. Windows: the Visual Studio Windows Debugger or GDB (using Cygwin or MinGW)

How do I enable breakpoints in VS Code?

Breakpoints. Breakpoints can be toggled by clicking on the editor margin or using F9 on the current line. Finer breakpoint control (enable/disable/reapply) can be done in the Run and Debug view's BREAKPOINTS section.


1 Answers

I had the same problem, not sure why, but setting "externalConsole" in launch.json to false did the trick for me.

This issue was reported on Microsoft/vscode-cpptools 15 days ago, has been occurring since v1.30.0, and is supposed to be fixed in the latest insiders build.

like image 164
nloomans Avatar answered Oct 18 '22 02:10

nloomans