gpt4 book ai didi

c - 使用 VS Code、Makefile 和自定义 bash 脚本在 C 语言中调试头文件

转载 作者:行者123 更新时间:2023-11-30 16:10:49 25 4
gpt4 key购买 nike

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

speller.c

#include "dictionary.h"

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

字典.c

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

Makefile

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 中使用它。

删除-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 文件中的方法?

最佳答案

当我从 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"
}
]
}

关于c - 使用 VS Code、Makefile 和自定义 bash 脚本在 C 语言中调试头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58793175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com