前言

VSVode是全世界最好用的编辑器

今天写C++的小程序突然觉得每次创建文件、创建调试的配置文件以及 Makefile 等很麻烦,于是想要创建一组文件模板,可以在每次创建项目的时候自动创建好需要的文件,经过搜索发现 vsc 的这个功能(用户代码),于是写下这篇博客记录一下。

vsc 默认可以创建一些语言专用的模板比如 c++、c#等,如下图:

kinds

但是我们也可以创建自定义的代码模板(全局使用),具体的操作如下:

如何创建

打开 VSCode,依次点击 文件-首选项-用户片段(英文为 File-Preference-UserSnippets),会出现上图中的选项

首先创建C++的模板(可以自定义)如下:

选择 C++ 选项,在其中粘贴以下内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//cpp.json
{
"C++ Snippets":{
"prefix": "C++", //在新建立的文件中输入C++,Tab或回车可以自动补全为以下 body 的内容
"body": [
"#include <iostream>",
"using namespace std;",
"",
"int main()",
"{",
"\t$0", //$0表示创建好后光标位置,$1表示tab后下一个位置
"\treturn 0;",
"}",
"",
],
"description": "A cpp file template." //提示信息
}
}

创建文件时文件内会有一些注释,可以看一下注释的内容,解释了如何创建模板以及一个示例。

此时在项目中创建一个 main.cpp 文件,在其中输入 C++ ,会出现如下提示:

cpp

回车或 Tab,会自动补全为以下内容(不要跟我说建议不要 using namespace std; 🤡):

cpp2

创建 Makefile 模板:

选择 New Global Snippets File,输入文件名,在其中填写以下内容

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
{
"gcc makefile": {
"prefix": "makefile_gcc",
"body": [
"CC=gcc",
"SRCS=$(wildcard *.c)",
"OBJS=$(patsubst %.c, %.o, $(SRCS))",
"FLAG=-g",
"NAME=$(wildcard *.c)",
"TARGET=$(patsubst %.c, %.exe, $(NAME))",
"",
"$(TARGET):$(OBJS)",
"\t$(CC) -o $@ $^ $(FLAG)",
"",
"%.o:%.c",
"\t$(CC) -o $@ -c $< -g",
"",
"clean:",
"\tdel $(TARGET) $(OBJS)"

],
"description": "gcc make file"
},
"g++ makefile": {
"prefix": "makefile_g++",
"body": [
"CC=g++",
"SRCS=$(wildcard *.cpp)",
"OBJS=$(patsubst %.cpp, %.o, $(SRCS))",
"FLAG=-g",
"NAME=$(wildcard *.cpp)",
"TARGET=$(patsubst %.cpp, %.exe, $(NAME))",
"",
"$(TARGET):$(OBJS)",
"\t$(CC) -o $@ $^ $(FLAG)",
"",
"%.o:%.c",
"\t$(CC) -o $@ -c $< -g",
"",
"clean:",
"\trm -rf $(TARGET) $(OBJS)"

],
"description": "g++ make file"
}
}

在项目中新建一个 Makefile 文件,输入 makefile_g++,自动补全后为以下内容:

g++

可以自动扫描当前目录中以 .cpp 结尾的文件,并进行编译。

具体参考 Makefile 万能模板。

创建调试所用的 task.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
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
67
68
{
"Cpp Make Task": {
"prefix": "task_make",
"body": [
"{",
"\t\"tasks\": [",
"\t{",
"\t\t\t\"type\": \"cppbuild\",",
"\t\t\t\"label\": \"Make\",",
"\t\t\t\"command\": \"D:\\\\\\\\Programs\\\\\\\\MinGW\\\\\\\\bin\\\\\\\\make.exe\",",
"\t\t\t\"args\": [",
"\t\t\t\t// \"-g\",",
"\t\t\t\t// \"\\${file}\",",
"\t\t\t\t// \"-o\",",
"\t\t\t\t// \"\\${fileDirname}\\\\\\\\\\${fileBasenameNoExtension}.exe\"",
"\t\t\t],",
"\t\t\t\"options\": {",
"\t\t\t\t\"cwd\": \"\\${workspaceFolder}\"",
"\t\t\t},",
"\t\t\t\"problemMatcher\": [",
"\t\t\t\t\"\\$gcc\"",
"\t\t\t],",
"\t\t\t\"group\": {",
"\t\t\t\t\"kind\": \"build\",",
"\t\t\t\t\"isDefault\": true",
"\t\t\t},",
"\t\t\t\"detail\": \"调试器生成的任务。\"",
"\t\t}",
"\t],",
"\t\"version\": \"2.0.0\"",
"}"
],
"description": "g++ debug task"
},
"Cpp g++ task": {
"prefix": "task_g++",
"body": [
"{",
"\t\"tasks\": [",
"\t{",
"\t\t\t\"type\": \"cppbuild\",",
"\t\t\t\"label\": \"g++\",",
"\t\t\t\"command\": \"D:\\\\\\\\Programs\\\\\\\\MinGW\\\\\\\\bin\\\\\\\\g++.exe\",",
"\t\t\t\"args\": [",
"\t\t\t\t\"-g\",",
"\t\t\t\t\"\\${file}\",",
"\t\t\t\t\"-o\",",
"\t\t\t\t\"\\${fileDirname}\\\\\\\\\\${fileBasenameNoExtension}.exe\"",
"\t\t\t],",
"\t\t\t\"options\": {",
"\t\t\t\t\"cwd\": \"\\${workspaceFolder}\"",
"\t\t\t},",
"\t\t\t\"problemMatcher\": [",
"\t\t\t\t\"\\$gcc\"",
"\t\t\t],",
"\t\t\t\"group\": {",
"\t\t\t\t\"kind\": \"build\",",
"\t\t\t\t\"isDefault\": true",
"\t\t\t},",
"\t\t\t\"detail\": \"调试器生成的任务。\"",
"\t\t}",
"\t],",
"\t\"version\": \"2.0.0\"",
"}"
],
"description": "g++ debug task"
}
}

创建 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
30
31
32
33
34
{
"g++ launch": {
"prefix": "launch",
"body": [
"{",
"\t\"version\": \"0.2.0\",",
"\t\"configurations\": [",
"\t\t{",
"\t\t\t\"name\": \"g++.exe - 生成和调试活动文件\",",
"\t\t\t\"type\": \"cppdbg\",",
"\t\t\t\"request\": \"launch\",",
"\t\t\t\"program\": \"\\${fileDirname}\\\\\\\\\\${fileBasenameNoExtension}.exe\",",
"\t\t\t\"args\": [],",
"\t\t\t\"stopAtEntry\": false,",
"\t\t\t\"cwd\": \"\\${workspaceFolder}\",",
"\t\t\t\"environment\": [],",
"\t\t\t\"externalConsole\": true,",
"\t\t\t\"MIMode\": \"gdb\",",
"\t\t\t\"miDebuggerPath\": \"D:\\\\\\\\Programs\\\\\\\\MinGW\\\\\\\\bin\\\\\\\\gdb.exe\",",
"\t\t\t\"setupCommands\": [",
"\t\t\t\t{",
"\t\t\t\t\t\"description\": \"为 gdb 启用整齐打印\",",
"\t\t\t\t\t\"text\": \"-enable-pretty-printing\",",
"\t\t\t\t\t\"ignoreFailures\": true",
"\t\t\t\t}",
"\t\t\t],",
"\t\t\t\"preLaunchTask\": \"Make$0\"",
"\t\t}",
"\t]",
"}"
],
"description": "g++ launch"
}
}

以上。