一. 在.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" } ] }
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" } ] }
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)