【问题标题】:How to fix ignored breakpoints when debugging C in VScode (gdb)?在 VScode (gdb) 中调试 C 时如何修复忽略的断点?
【发布时间】:2022-11-17 17:28:47
【问题描述】:

我正在尝试使用断点在 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

【问题讨论】:

    标签: debugging visual-studio-code gdb


    【解决方案1】:

    您的 makefile 有很多问题。首先,你构建的目标是all,它依赖于dynamic_array.o和test.o,但是构建这些没有规则,只有构建dynamic_array和test的规则。

    因此,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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 2013-04-03
      • 2014-01-15
      • 1970-01-01
      • 2018-06-24
      • 2014-04-03
      • 1970-01-01
      相关资源
      最近更新 更多