写在前面

感觉现在很多非计算机专业的朋友也有很多写代码的需求,最近几个月陆续有些朋友问了我一些有关于C++开发环境配置、代码编辑器选择相关的问题。

由于网上已有很多教程,所以在这里主要还是分享一些我在配置 C++ 开发环境过程中的经验,并对各个网络教程做一个汇总。

从0到1

不论你的用途是什么,代码编辑器首选 vscode。

理由很简单:1、免费。2、对于非计算机行业从业者来说,能涵盖所有需求。

windows配置vscode C++ 开发环境的教程
mac配置vscode C++ 开发环境的教程

windows需要用户自己下载 gcc/g++ 编译器,按照教程下载 MinGW 即可;macOS 自带了 clang 编译器,配置过程稍简单一些。

主题和字体

FiraCode 是一个非常好看的字体,并且可以支持连字符(当然不喜欢用连字符也禁掉)

FiraCode配置教程

可以在VScode的扩展中下载各式各样的主题,笔者使用的是 Dracula Official

扩展

在上面的 vscode 配置教程中已经安装了 C++ 开发环境所必须的的插件了。其他的可以自己在使用过程中按需安装。可以看看 这里

快捷键

进入 左下角【管理】-【键盘快捷方式】- 右上角【打开键盘快捷方式】

可以直接将 keybindings.json 覆盖为下列 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Place your key bindings in this file to override the defaultsauto[]
[
// 放大字体
{
"key": "cmd+1",
"command": "editor.action.fontZoomIn"
},

// 缩小字体
{
"key": "cmd+2",
"command": "editor.action.fontZoomOut"
},

// 将整行向上移动
{
"key": "shift+cmd+[",
"command": "editor.action.moveLinesUpAction",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "alt+up",
"command": "-editor.action.moveLinesUpAction",
"when": "editorTextFocus && !editorReadonly"
},

// 将整行向下移动
{
"key": "shift+cmd+]",
"command": "editor.action.moveLinesDownAction",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "alt+down",
"command": "-editor.action.moveLinesDownAction",
"when": "editorTextFocus && !editorReadonly"
},

// 运行代码
{
"key": "shift+cmd+r",
"command": "code-runner.run"
},
{
"key": "ctrl+alt+n",
"command": "-code-runner.run"
},

// 将光标聚焦到编辑器
{
"key": "shift+cmd+up",
"command": "workbench.action.focusPreviousGroup"
},

// 将光标聚焦到终端
{
"key": "shift+cmd+down",
"command": "workbench.action.terminal.focus"
},

// 通过tab进行代码补全
{
"key": "tab",
"command": "acceptSelectedSuggestion",
"when": "suggestWidgetVisible && textInputFocus"
},
{
"key": "shift+tab",
"command": "acceptSelectedSuggestion",
"when": "suggestWidgetVisible && textInputFocus"
},
{
"key": "tab",
"command": "selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions &&editorTextFocus && suggestWidgetVisible"
},
{
"key": "shift+tab",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && editorTextFocus && suggestWidgetVisible"
},
{
"key": "cmd+down",
"command": "-workbench.action.terminal.focus",
"when": "accessibilityModeEnabled && accessibleViewOnLastLine && terminalHasBeenCreated && accessibleViewCurrentProviderId == 'terminal' || accessibilityModeEnabled && accessibleViewOnLastLine && terminalProcessSupported && accessibleViewCurrentProviderId == 'terminal'"
},
{
"key": "shift+cmd+down",
"command": "-workbench.action.terminal.selectToNextCommand",
"when": "terminalFocus && terminalHasBeenCreated || terminalFocus && terminalProcessSupported"
},
{
"key": "shift+cmd+down",
"command": "-cursorBottomSelect",
"when": "textInputFocus"
},
{
"key": "shift+cmd+up",
"command": "-workbench.action.terminal.selectToPreviousCommand",
"when": "terminalFocus && terminalHasBeenCreated || terminalFocus && terminalProcessSupported"
},
{
"key": "shift+cmd+up",
"command": "-list.collapseAll",
"when": "listFocus && !inputFocus && !treestickyScrollFocused"
},
{
"key": "shift+cmd+up",
"command": "-cursorTopSelect",
"when": "textInputFocus"
}
]

设置

进入 左下角【管理】- 【设置】- 右上角【打开设置】

如果懒得研究每个条目的具体含义,可以直接将 settings.json 覆盖为下列 json 代码,保存后即可生效。由于包含了很多笔者自己的插件的配置或者字体,如果遇到不能生效的直接删去即可,着重看带了注释的条目。

注意有些配置是基于 clang 或者本地文件的,如果是 g++ 编译器,请记得更改。

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{
// 设置定时自动保存
"files.autoSave": "afterDelay",
"workbench.iconTheme": "material-icon-theme",
"code-runner.runInTerminal": true,
"files.defaultLanguage": "html",
"explorer.confirmDelete": false,
"editor.detectIndentation": false,
"editor.fontLigatures": false,
"editor.fontWeight":"500" ,
"files.autoGuessEncoding": true,
"maven.excludedFolders": [
"**/.*",
"**/node_modules",
"**/target",
"**/bin"
],
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"C_Cpp.commentContinuationPatterns": [
"/**"
],
"C_Cpp.files.exclude": {
"**/.vscode": true
},
"security.workspace.trust.untrustedFiles": "open",
"cmake.configureOnOpen": true,
"terminal.integrated.enableMultiLinePasteWarning": false,
"workbench.colorTheme": "Dracula Soft",
"bracketPairColorizer.depreciation-notice": false,
// 自动保存时间间隔长度
"files.autoSaveDelay": 100,
"editor.autoClosingBrackets": "always",
"C_Cpp.default.cppStandard": "c++17",
"[python]": {
"editor.formatOnType": true
},
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"zig": "zig run",
// 一键运行 C++ 时的命令,基于 clang 编译器
// 如果是 g++, 把这一条的 clang++ 改成 g++ 即可
"cpp": "cd $dir && clang++ --std=c++17 $fileName -o $fileNameWithoutExt -Wall && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
"perl": "perl",
"perl6": "perl6",
"ruby": "ruby",
"go": "go run",
"lua": "lua",
"groovy": "groovy",
"powershell": "powershell -ExecutionPolicy ByPass -File",
"bat": "cmd /c",
"shellscript": "bash",
"fsharp": "fsi",
"csharp": "scriptcs",
"vbscript": "cscript //Nologo",
"typescript": "ts-node",
"coffeescript": "coffee",
"scala": "scala",
"swift": "swift",
"julia": "julia",
"crystal": "crystal",
"ocaml": "ocaml",
"r": "Rscript",
"applescript": "osascript",
"clojure": "lein exec",
"haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
"racket": "racket",
"scheme": "csi -script",
"ahk": "autohotkey",
"autoit": "autoit3",
"dart": "dart",
"pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
"d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
"haskell": "runghc",
"nim": "nim compile --verbosity:0 --hints:off --run",
"lisp": "sbcl --script",
"kit": "kitc --run",
"v": "v run",
"sass": "sass --style expanded",
"scss": "scss --style expanded",
"less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
"FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"sml": "cd $dir && sml $fileName"
},
// C语言格式
"C_Cpp.clang_format_style": "{BasedOnStyle: Chromium, IndentWidth: 4}",
"git.ignoreLegacyWarning": true,
"editor.wordWrap": "on",
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.unicodeHighlight.ambiguousCharacters": false,
"editor.fontFamily": "Fira Code",
"window.zoomLevel": 1,
"[cpp]": {
"editor.defaultFormatter": "ms-vscode.cpptools"
},
"redhat.telemetry.enabled": true,
"terminal.integrated.fontFamily": "MesloLGS NF",
"git.autofetch": true,
"[markdown]": {
"editor.defaultFormatter": "yzhang.markdown-all-in-one"
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
}
}

用户代码片段

进入 左下角【管理】-【用户代码片段】,然后创建 C++ 的用户代码片段。

将整个文件覆盖为如下代码即可:

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
{
"cpp":{
"prefix": "xcpc",
"body":[
"#include <algorithm>",
"#include <cstdio>",
"#include <iostream>",
"#include <map>",
"#include <set>",
"#include <vector>",
"#define all(x) x.begin(), x.end()",
"typedef int64_t ll;",
"typedef std::pair<int, int> pii;",
"typedef std::pair<ll, int> pli;",
"typedef std::pair<ll, ll> pll;",
"typedef double db;",
"typedef long double ldb;",
"const int maxN = 2e5 + 5;",
"",
"int main() {",
" freopen(\"input.txt\", \"r\", stdin);",
" // freopen(\"output.txt\", \"w\", stdout);",
" std::ios::sync_with_stdio(false);",
" std::cin.tie(NULL);",
"",
" int T = 1;",
" std::cin >> T;",
" while (T--) {",
" }",
"",
" return 0;",
"}",
""
],
"description": "cpp-templte"
}
}

以后输入 “xcpc” ,再回车,就可以直接打出来这些代码。当然读者可以根据自己的兴趣更改。