15

I am debugging a c++ code in VS Code but it doesn't stop on breakpoint and visualize the variables, watch and call stack, which it was supposed to do. Instead of this, it prints this in debug console:

Breakpoint 1, 0x000000000040074a in main ()
[Inferior 1 (process 9445) exited normally]
The program '/home/hashir/x/a.out' has exited with code 0 (0x00000000)

here is launch.json file:

{
    // 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": "/home/hashir/x/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "/home/hashir/x/",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
Hashir Sarwar
  • 1,115
  • 2
  • 14
  • 28

5 Answers5

35

Compile the program using the -g tag along with g++/clang++.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
Hashir Sarwar
  • 1,115
  • 2
  • 14
  • 28
3

This problem literally ruined my day. It turned out that all I had to do is just Terminal > Run Build Task or Ctrl+Shift + B and then start debugging.

temmmy
  • 31
  • 3
1

I've figured out that if your source-code file name contains white spaces, like "Binary Search.cpp", VSCode will ignore the breakpoints regardless of "stop at entry" configuration. Removing the white spaces from my source files' names worked, although their paths contain white spaces. For example "C++ Exercises/BinarySearch.cpp" seems to be valid.

In this issue opened on GitHub, it is suggested that non-ascii characters might cause such problems.

0

The default launch.json file has this line:
"program": "${workspaceFolder}/a.out",
But this will run a.out to start debugging, and if you configured tasks.json file with only one cpp file, it will have lines like:

                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"  

this will create an executable with the same name with your current file.
Either change the tasks.json to create an a.out file or change launch.json file with the no extension name of your current file.

launch.json:

"program": "${workspaceFolder}/<stripped current file name>", 

OR
tasks.json:

                "-o",
                "${fileDirname}/a.out"  
Sahin
  • 1,032
  • 14
  • 23
0

It seem something wrong with environment vars, try open vscode from "developer command prompt for VScode", then on prompt, type: code