VS code配置编译及debug环境

一. 在.vscode下建两个文件

     1.1. tasks.json:配置编译

     1.2. lunch.json配置debug 

           PS:附录中我提供源文件

 

二.   创建makefile,以便json调用

三.  build

        3.1. 打开源码文件

        3.2. 编译

                 点击菜单上Terminal->config Task->make build

       3.2. clean

                 点击菜单上Terminal->config Task->clean

 

 四. Debug

    4.1. 按F5开始debug(或按菜单上的Run)

    4.2. 设置断点(在行前左击)

 

     4.3. 如果运行时不执行断点,报信息如下

         4.3.1. Module containing this breakpoint has not yet loaded or the breakpoint address not be obtained

                如果你编译没有加-g时,调试就会出现这个信息

 五. 附录

    5.1. tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make build",
            "command": "/usr/bin/make",
            "args": [
                "all"
            ],
            "type": "shell",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "command": "/usr/bin/make",
            "args": [
                "clean"
            ],
            "type": "shell"
        },
        {
            "type": "shell",
            "label": "gcc build",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/main"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}
View Code

    5.2. lunch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "build debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/main",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            //"preLaunchTask": "build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
View Code

    5.3 Makefile

CC = /usr/bin/gcc
LD = /usr/bin/ld

# 正则表达式表示目录下所有.c文件,相当于:SRCS = main.c a.c b.c
SRCS = $(wildcard *.c)

# OBJS表示SRCS中把列表中的.c全部替换为.o,相当于:OBJS = main.o a.o b.o
OBJS = $(patsubst %c, %o, $(SRCS))

INC = -I ./

CFLAGS = 

# 可执行文件的名字
TARGET = main

# .PHONE伪目标,具体含义百度一下一大堆介绍
.PHONY:all debug clean

# 要生成的目标文件
all: $(TARGET)
# 第一行依赖关系:冒号后面为依赖的文件,相当于Hello: main.o a.o b.o
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
$(TARGET): $(OBJS)
    $(CC) -o $@ $^
# 上一句目标文件依赖一大堆.o文件,这句表示所有.o都由相应名字的.c文件自动生成
%o:%c
    $(CC) -c $^ $(CFLAGS) $(INC)


    # make clean删除所有.o和目标文件
clean:
    rm -f $(OBJS) $(TARGET)
View Code

 

posted @ 2020-11-29 23:40  三七鸽  阅读(4651)  评论(1编辑  收藏  举报