使用VS Code,Makefile和自定义bash脚本在C lang中调试头文件

我正在尝试使用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脚本,因此我可以快速获取最新的.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文件中的方法?

sunshine7993 回答:使用VS Code,Makefile和自定义bash脚本在C lang中调试头文件

当我从speller.c的{​​{1}}函数中进入加载时,调试器现在可以工作了。这是有效的配置文件:

tasks.json

main

launch.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"
        }
    ]
}
本文链接:https://www.f2er.com/3127472.html

大家都在问