我需要一种通过vscode中的makefile调试包含多个C文件的C程序的方法,当我在task.json中使用wingw32-make命令调试该程序时,该程序将直接运行而不会在断点处停止,但是在task.json中使用gcc命令时,可以到达断点。我很困惑。原谅我的无知,任何回应将不胜感激!
下面是我的代码
heada.h
#ifndef PRACTICES_MYFUNC0A_H
#define PRACTICES_MYFUNC0A_H
enum steerwheel {square, circle};
typedef struct {
int wheels;
enum steerwheel sw;
} Car;
Car * getCar(int, enum steerwheel);
#endif //PRACTICES_MYFUNC0A_H
MainTest.c
#include <stdio.h>
#include <malloc.h>
#include "funcha.h"
int main() {
Car *car = getCar(4, square);
int i;
printf("%d,%d\n",car->wheels, car->sw);
scanf("%d",&i);
printf("%d\n", i);
free(car);
getchar();
}
MyFunc01.c
#include <stdio.h>
#include <malloc.h>
#include "funcha.h"
Car * getCar(int wheels, enum steerwheel sw) {
Car *car = malloc(sizeof(Car));
car->wheels = wheels;
car->sw = sw;
return car;
}
Makefile
MainTest: MainTest.o myFunc01.o funcha.h
gcc -o MainTest MainTest.o myFunc01.o
myFunc01.o: myFunc01.c funcha.h
gcc -c myFunc01.c
MainTest.o: MainTest.c funcha.h
gcc -c MainTest.c
.PHONY: clean
clean:
rm MainTest myFunc01.o MainTest.o
和.vscode foloer的文件如下:launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\work_softwares\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "makerun"
}
]
}
tasks.json
{
"tasks": [
{
"type": "shell",
"label": "makerun",
// this manner can arrived at the breakpoints。 why ???????
"command": "D:\\work_softwares\\mingw64\\bin\\mingw32-make.exe",
"args": [
"-C",
"${workspaceFolder}",
"MainTest"
],
"options": {
"cwd": "D:\\work_softwares\\mingw64\\bin"
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "notmakerun",
// this manner can arrived at the breakpoints。
"command": "D:\\work_softwares\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${workspaceFolder}\\*.c",
"-o",
"${workspaceFolder}\\MainTest.exe"
],
"options": {
"cwd": "D:\\work_softwares\\mingw64\\bin"
},
"problemMatcher": []
}
],
"version": "2.0.0"
}
在Makefile编译命令中,您没有将调试标志(例如-g
或-ggdb
)传递给gcc调用。因此,您不能在通过Makefile生成的构建中放置断点。