VS code is ignoring the breakpoint in c++ debugging
Asked Answered
C

9

18

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
                }
            ]
        }
    ]
}
Colton answered 15/1, 2018 at 17:49 Comment(1)
"stopAtEntry": false should it be "stopAtEntry": true?Emmetropia
C
39

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

Colton answered 15/1, 2018 at 18:58 Comment(4)
Thank you, I was facing the same issue. Your answer helped me.Cromlech
Any extra clarification on where to enter this tag specifically? Thanks.Jackpot
@Jackpot add that flag when you are compiling, as in running the g++ file.cpp command.Protecting
@Jackpot In the file tasks.json add the -g option under tasks->args (for example see my answer)Dirham
E
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.

Equilateral answered 8/5, 2021 at 15:36 Comment(1)
wtf! this solved my problem too! eventhough there is a preLaunchTask tag in launch.json. Thanks!Generally
I
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.

Interfaith answered 15/2, 2020 at 22:0 Comment(0)
A
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"  
Andi answered 13/4, 2022 at 17:55 Comment(0)
W
0

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

Wallenstein answered 12/7, 2022 at 0:9 Comment(0)
K
0

In my case, this was happening when building manually with cmake in a separate terminal, and then running the binary using a Docker development environment within VSCode.

Removing the build directory and reconfiguing/rebuilding within the development environment fixed the issue.

Khorma answered 4/9, 2023 at 10:36 Comment(0)
D
0

In the file tasks.json add the -g option under tasks->args.

For example:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\ProgramData\\msys64\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
...
Dirham answered 18/9, 2023 at 13:50 Comment(0)
C
0

In VS Code, when debugging, breakpoints set in source files will be ignored if the sections of code containing these breakpoints are conditionally executed (e.g. selection-statement and/or iteration-statement) and the condition always evaluates to false. For example due to a bug.

Contusion answered 25/2, 2024 at 0:22 Comment(0)
F
0

Add the below to CMakeLists.txt:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
Furgeson answered 22/3, 2024 at 15:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.