debugging 如何修复在VScode(gdb)中调试C时忽略的断点?

qij5mzcb  于 2022-11-24  发布在  Vscode
关注(0)|答案(1)|浏览(243)

我尝试使用断点在vscode中调试我的C,但每次运行调试器时,调试器似乎都跳过了断点(断点的颜色从红色变成灰色)。我看了这个question,它本质上是我的同一个问题。我试了那里所有的答案,没有一个有效(没有一个被提问的人设置为“答案”,因此我再次问这个问题)。所以我的问题是,如何让vscode断点在C中工作?
Vscode版本:在Windows 10上为1.73.1
gdb版本:12.1

启动.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",
}
]

任务.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"

}

生成文件

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
7rtdyuoh

7rtdyuoh1#

你的makefile有很多问题。首先,你构建的目标是all,它依赖于dynamic_array.otest.o,但是,没有构建这些的规则,只有构建dynamic_arraytest的规则。
因此,make将使用其默认规则来构建.o文件,即:

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

这导致了下一个问题,这可能就是您看到的断点问题的原因-无论是隐式规则还是您试图编写的规则,都不包括gcc-g标志。-g标志打开调试信息的生成。
我至少会将makefile重写为:

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

相关问题