Home > Mobile >  How to fix ignored breakpoints when debugging C in VScode (gdb)?
How to fix ignored breakpoints when debugging C in VScode (gdb)?

Time:11-18

I am trying to debug my C in vscode using breakpoints, but the debugger seems to skip them everytime i run it (the break points change colours from red to grey). I looked at this question which is essentially the same question i have. I tried all the answers there and none worked (none were set as 'answers' by the person who asked, hence why i am asking this question again). So my question is, how to get vscode breakpoints working in C?

Vscode version: 1.73.1 on windows 10

gdb version: 12.1

launch.json

{
"configurations": [
{
    "name": "(gdb) Launch",
    "type": "cppdbg",
    "request": "launch",
    "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${fileDirname}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
    "setupCommands": [
        {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        },
        {
            "description":  "Set Disassembly Flavor to Intel",
            "text": "-gdb-set disassembly-flavor intel",
            "ignoreFailures": true
        }
    ],
    "preLaunchTask": "C/C  : gcc.exe build active file",
}
]

tasks.json

{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C  : gcc.exe build active file",
        "command": "make",
        "args": [
            "all"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
    
],
"version": "2.0.0"

}

makefile

dynamic_array: dynamic_array.c dynamic_array.h
    gcc -c dynamic_array.c
test: test.c dynamic_array.h
    gcc -c test.c
all: dynamic_array.o test.o
    gcc -o test.exe dynamic_array.o test.o
clean:
    del -f *.o & del -f *.exe & del -f *.out

CodePudding user response:

Your makefile has a number of problems. First, the target you build is all, which depends on dynamic_array.o and test.o, however, there is no rule for building these, only a rule for building dynamic_array and test.

As such, make will use its default rule for building a .o file, which is:

$(CC) $(CPPFLAGS) $(CFLAGS) -c

This leads to the next problem, which is probably the cause of the breakpoint problems you are seeing - neither the implicit rule, nor the rule you tried to write, include the -g flag for gcc. The -g flag turns on production of debug information.

I would at a minimum, rewrite the makefile as:

dynamic_array.o: dynamic_array.c dynamic_array.h
    gcc -c -g3 dynamic_array.c
test.o: test.c dynamic_array.h
    gcc -c -g3 test.c
all: dynamic_array.o test.o
    gcc -g3 -o test.exe dynamic_array.o test.o
clean:
    del -f *.o & del -f *.exe & del -f *.out
  • Related