0%

VSCode配置C++多文件环境

修正

发现makefile存在问题,仅会编译比main.cpp更新的依赖文件,导致每次都要make两次才能正常调试。已修改$?$^

[email protected] –代表目标文件(target)
$^ –代表所有的依赖文件(components)
$< –代表第一个依赖文件(components中最左边的那个)
$? –代表比目标还要新的依赖文件列表

概述

之前从知乎抄的配置只能编译单文件源文件,编译多文件的话需要手动修改g++编译参数(不能忍啊→_→),于是搜索之,找到解决方案。
关键词:makefile

环境准备

原本使用VSCode就可以正常进行编译调试等操作。
已安装make工具并添加到环境变量,我自己用的是mingw32-make
(也可使用msys提供的工具包,度盘链接: https://pan.baidu.com/s/1zJBcS6tjfXlwg1kP6VnLSg 提取码: f9ya)

具体方案

构建makefile模板

我基本是直接复制参考博文里的模板(修改标准库为17),内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# originating https://github.com/TheNetAdmin/Makefile-Templates
# tool marcros
CC := g++
CCFLAG := -std=c++17
DBGFLAG := -g
CCOBJFLAG := $(CCFLAG) -c

# path marcros
BIN_PATH := bin
OBJ_PATH := obj
SRC_PATH := src
DBG_PATH := debug

# compile marcros
TARGET_NAME := main
ifeq ($(OS),Windows_NT)
TARGET_NAME := $(addsuffix .exe,$(TARGET_NAME))
endif
TARGET := $(BIN_PATH)/$(TARGET_NAME)
TARGET_DEBUG := $(DBG_PATH)/$(TARGET_NAME)
MAIN_SRC := src/main.cpp

# src files & obj files
SRC := $(foreach x, $(SRC_PATH), $(wildcard $(addprefix $(x)/*,.c*)))
OBJ := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
OBJ_DEBUG := $(addprefix $(DBG_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))

# clean files list
DISTCLEAN_LIST := $(OBJ) \
$(OBJ_DEBUG)
CLEAN_LIST := $(TARGET) \
$(TARGET_DEBUG) \
$(DISTCLEAN_LIST)

# default rule
default: all

# non-phony targets
$(TARGET): $(OBJ)
$(CC) $(CCFLAG) -o [email protected] $^

$(OBJ_PATH)/%.o: $(SRC_PATH)/%.c*
$(CC) $(CCOBJFLAG) -o [email protected] $<

$(DBG_PATH)/%.o: $(SRC_PATH)/%.c*
$(CC) $(CCOBJFLAG) $(DBGFLAG) -o [email protected] $<

$(TARGET_DEBUG): $(OBJ_DEBUG)
$(CC) $(CCFLAG) $(DBGFLAG) $^ -o [email protected]

# phony rules
.PHONY: all
all: $(TARGET)

.PHONY: debug
debug: $(TARGET_DEBUG)

.PHONY: clean
clean:
@echo CLEAN $(CLEAN_LIST)
@rm -f $(CLEAN_LIST)

.PHONY: distclean
distclean:
@echo CLEAN $(CLEAN_LIST)
@rm -f $(DISTCLEAN_LIST)

目录结构

src文件夹下放源文件以及头文件,后面还要需要修改.vscode中的task.jsonlaunch.json
注意:如果使用本文中的makefle,包含主函数的源文件名必须为main.cpp

task.json

如果你使用的是msys的工具包,那command就写make
按快捷键Ctrl+Shift+B可以执行仅生成命令(build)
(在微软输入法中文模式时会出现表情窗口←_←)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "mingw32-make",
"args": [
"default"
],
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
},
{
"label": "build-debug",
"command": "mingw32-make",
"args": [
"debug"
],
"type": "shell"
},
{
"label": "clean",
"command": "mingw32-make",
"args": [
"clean"
],
"type": "shell"
}
]
}

launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug/main.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/debug/",
"environment": [],
"externalConsole": true,
"internalConsoleOptions": "neverOpen",
"MIMode": "gdb",
"miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则去掉
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false,
}
],
"preLaunchTask": "build-debug" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
}
]
}

配置完成

enjoy it!
😉

参考

VS Code 配置 C/C++ 环境