Reputation: 11
I need a way to debug a C program consisting of multi C files with the makefile in vscode, when I debug the program using the wingw32-make command in the tasks.json , the program run directly without stoping at the breakpoints, but when using the gcc command in the tasks.json the breakpoints can be arrived. I am confused. Forgive my ignorance, any response would be greatly appreciated!
below is my code
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
and the .vscode foloer's file is below: 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"
}
Upvotes: 0
Views: 2301
Reputation: 366
In your Makefile compilation command, you're not passing debug flags, like -g
or -ggdb
to the gcc invocation. So you can't put a breakpoint in the build generated via Makefile.
But in tasks.json, specifically the task labelled with notmakerun
, you are passing -g
flag. So build will be generated with debug symbols, and so you can debug it.
In a nutshell, try adding -g
into the all of the gcc
commands in the Makefile
Upvotes: 2