全网最简单vscode使用Makefile调试多文件的C/C++代码

本文详细介绍了如何在VSCode中使用Makefile配置和调试C/C++代码,包括设置launch.json和tasks.json文件,以便于在多文件项目中轻松进行调试。
摘要由CSDN通过智能技术生成

前言

vscode调试C/C++教程很多,操作麻烦,这里试图找到一个最简单的使用vscode调试C/C++代码的方法。这里是使用Makefile的多文件方式。

测试文件

tree 
.
├── func.c
├── func.h
├── main.c
└── Makefile

fun.c

#include <stdio.h>
#include "func.h"

int foo1(int a)
{
    int b = ++a;
    printf("This is foo1 %d\n",b);
}

fun.h

int foo1(int a);

main.c

#include <stdio.h>
#include "func.h"

int main()
{
    int a = 1;
    printf("Hello, I am coming %d\n", a);

    foo1(a);
    return 0;
}

Makefile

CC = gcc
CFLAGS = -g
LDFLAGS = 

TARGET = test
SRCS = $(wildcard *.c)

OBJS = $(SRCS:.c=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) -o $(TARGET) $(LDFLAGS)

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	rm -f $(OBJS) $(TARGET)

关键配置文件

在.vscode路径下

lauch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/test",  //编译后可执行文件路径
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask" :"C/C++: gcc 生成活动文件", // 与task中label一致
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc 生成活动文件",
            "command": "make",               // 使用Mafile编译
            "args": [
                //"-fdiagnostics-color=always",
                //"-g",
                //"${file}",
                //"-o",
                //"${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"  //项目所在目录
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

最终调试时文件

/ws/example$ tree -a
.
├── func.c
├── func.h
├── func.o
├── main.c
├── main.o
├── Makefile
├── test
└── .vscode
    ├── launch.json
    ├── settings.json
    └── tasks.json

成功调试

在这里插入图片描述

使用VSCode调试包含多个MakefileC++项目之前,需要先确保已安装以下工具: 1. C/C++插件 2. Make工具 3. GDB调试器 接下来,按照以下步骤进行操作: 1. 打开C++项目所在的文件夹,按下F5或者点击左侧调试栏中的“启动调试”按钮。 2. 在弹出的“选择环境”窗口中,选择“C++ (GDB/LLDB)”环境,然后点击“创建配置文件”。 3. 在打开的“launch.json”文件中,添加以下配置: ``` { "name": "Debug Multi Makefile Project", "type": "cppdbg", "request": "launch", "program": "${workspaceRoot}/path/to/executable", "args": [], "stopAtEntry": true, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/path/to/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "Set breakpoint at main", "text": "break main", "ignoreFailures": true } ], "preLaunchTask": "build" } ``` 其中,需要将“program”字段的值设置为项目生成的可执行文件的路径,将“miDebuggerPath”字段的值设置为GDB调试器的路径,将“setupCommands”字段中的“text”值设置为需要执行的GDB命令,比如“break main”可以在程序开始执行时自动停在main函数处。 4. 在VSCode中打开项目的“tasks.json”文件,在该文件中配置需要执行的Makefile命令,比如: ``` { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "make -f makefile1 && make -f makefile2", "group": { "kind": "build", "isDefault": true } } ] } ``` 其中,“command”字段的值为需要执行的Makefile命令,可以包括多个Makefile。 5. 按下F5或者点击左侧调试栏中的“启动调试”按钮,等待程序运行到设置的断点处即可开始调试。 希望这些步骤能够对你有所帮助。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值