【问题标题】:Debug header files in C lang with VS Code, Makefile and custom bash-script使用 VS Code、Makefile 和自定义 bash 脚本在 C 语言中调试头文件
【发布时间】:2019-11-10 21:39:39
【问题描述】:

我正在尝试使用 VS Code 调试 C 程序,该程序通过头文件加载方法并使用 Makefile 进行编译。我想在其他 C 文件中放置断点,以查看我的方法是否正常工作。

speller.c

#include "dictionary.h"

int main(int argc, char *argv[])
{
    bool loaded = load(dictionary);
}

dictionary.c

bool load(const char *dictionary)
{
 // BREAKPOINT!
}

生成文件

speller:
    gcc -g -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow -c -o speller.o speller.c
    gcc -g -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow -c -o dictionary.o dictionary.c
    gcc -g -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow -o speller speller.o dictionary.o

这是我制作的自定义 bash-script,这样我就可以快速获取最新的.out 文件和编译代码,而不必手动重新运行所有步骤。

这是可执行的,因此我可以稍后在我的tasks.json 中使用它。

delete-make-run.sh

#!/bin/bash

rm -f \*.o && rm -f $1 && make && ./$1 $2 $3

launch.json

{
            "name": "gcc build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/speller",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc delete make run",
            "miDebuggerPath": "/usr/bin/gdb"
        }

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc delete make run",
            "command": "delete-make-run",
            "args": [
                "speller",
                "texts/aca.txt"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

像这样运行我的程序:

$ delete-make-run speller texts/aca.txt

当我在 speller.c 中放置断点时,它可以工作,但是如何调试 .h 文件中的方法?

【问题讨论】:

  • 方法不在头文件中——这只是声明。只需将断点放在要调试的代码中即可。目前尚不清楚为什么您认为头文件与此相关。尝试从main() 踏入进入 load() 开始。您将看到它进入了定义.c 中的函数definition。您的 makefile 是不寻常的 - 并且可能不会超过批处理文件 - 对于较大的项目,您将希望利用 make 的功能来仅重建需要重建的组件。

标签: c bash makefile vscode-debugger


【解决方案1】:

当我从speller.cmain 函数步入进入 加载时,调试器现在可以工作了。以下是工作配置文件:

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "gcc delete make run",
            "command": "del-make-run",
            "args": [],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/speller",
            "args": [
                "speller",
                "texts/aca.txt"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc delete make run",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2017-09-26
    • 2020-10-12
    相关资源
    最近更新 更多