簡體   English   中英

無法在 vscode 中使用 makefile 調試 C

[英]can't debug C with makefile in vscode

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 tasks.json中的gcc命令可以到達斷點。 我很困惑。 原諒我的無知,任何回應將不勝感激!

下面是我的代碼

頭文件

#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"
        }

    ]
}

任務.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 生成的構建中放置斷點。

但是在 tasks.json,特別是標有notmakerun的任務中,您正在傳遞-g標志。 因此,將使用調試符號生成構建,因此您可以對其進行調試。

簡而言之,嘗試將-g添加到 Makefile 中的所有gcc命令中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM