VS Code 配置教程

· · 科技·工程

面向 OIer 的 VS Code 配置教程。

1. 安装

1.0. 安装 Motrix(可选)

https://motrix.app

安装后就可以用 Motrix 快速下载了。

1.1. 安装 VS Code

https://code.visualstudio.com

最后一个支持 Windows 7 的版本:https://code.visualstudio.com/updates/v1_70

1.2. 安装 MinGW-w64

https://winlibs.com

下载 Release versions 里第一个标有 (LATEST) 的版本。

默认会附带 LLVM,如果你不需要可以选择 without LLVM/Clang/LLD/LLDB 的版本。

解压到合适的位置,然后将其中的 mingw64/bin 添加到环境变量即可。

1.3. 安装 Git Bash

https://git-scm.com/download/win

最后一个支持 Windows 7 的版本:\ https://github.com/git-for-windows/git/releases/tag/v2.46.2.windows.1

安装 Git Bash 是因为 Powershell 太难用了。

1.4. 安装字体

1.4.1. Fira Code

https://github.com/tonsky/FiraCode

解压后安装 ttf 下的所有字体即可。

1.4.2. Noto Sans CJK

https://github.com/notofonts/noto-cjk/blob/main/Sans/README.md

解压后直接安装即可。

2. 配置

2.1. 安装扩展

打开 VS Code 然后安装以下扩展:

  1. C/C++
  2. Chinese (Simplified) Language Pack for Visual Studio Code
  3. Code Runner
  4. Competitive Programming Helper (cph)
  5. Error Lens
  6. Fluent Icons
  7. Markdown All in One
  8. One Dark Pro
  9. Prettier
  10. vscode-icons
  11. vscode-pdf

有些教程说要配置 launch.json tasks.json 才能运行,但是这样太麻烦了。

我们安装 Code Runner 扩展后直接 Ctrl+Alt+N 就可以了,或者你可以直接用 cph。

2.2. 创建文件夹

在一个合适的位置创建一个合适的文件夹用于存储你所有的 C++ 文件。

然后还需要创建一些配置文件,创建完成后目录结构应为如下:

.
├── .vscode
│   └── settings.json
└── .clang-format

实际上你也可以不创建而是使用全局的 settings.json(其实这也是推荐的做法)。

直接按 Ctrl+Shift+P 然后输入 >workbench.action.openSettingsJson 就可以打开了。

2.3. 配置文件

配置文件的内容:

// settings.json
{
    "[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
    "[jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
    "[markdown]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
    "C_Cpp.autoAddFileAssociations": false,
    "code-runner.executorMap": {
        "cpp": "g++ $fileName -o $fileNameWithoutExt -std=c++23 -Wall -Wextra -O2 -march=native && ./$fileNameWithoutExt"
    },
    "code-runner.fileDirectoryAsCwd": true,
    "code-runner.ignoreSelection": true,
    "code-runner.runInTerminal": true,
    "cph.general.autoShowJudge": false,
    "cph.language.cpp.Args": "-std=c++23 -Wall -Wextra -O2 -march=native",
    "editor.cursorBlinking": "smooth",
    "editor.cursorSmoothCaretAnimation": "on",
    "editor.fontFamily": "'Fira Code', 'Noto Sans CJK SC'",
    "editor.fontLigatures": "'cv01', 'cv02', 'zero'",
    "editor.minimap.renderCharacters": false,
    "editor.minimap.scale": 2,
    "editor.rulers": [100],
    "editor.stickyScroll.enabled": false,
    "editor.unicodeHighlight.ambiguousCharacters": false,
    "editor.wordWrap": "on",
    "explorer.autoReveal": false,
    "files.associations": { ".clang*": "yaml" },
    "files.autoGuessEncoding": true,
    "markdown.preview.fontFamily": "'Noto Sans CJK SC'",
    "prettier.printWidth": 100,
    "prettier.tabWidth": 4,
    "search.followSymlinks": false,
    "terminal.integrated.defaultProfile.windows": "Git Bash",
    "terminal.integrated.enableMultiLinePasteWarning": "never",
    "terminal.integrated.fontFamily": "'Fira Code', 'Noto Sans CJK SC'",
    "workbench.colorTheme": "One Dark Pro Mix",
    "workbench.iconTheme": "vscode-icons",
    "workbench.productIconTheme": "fluent-icons",
    "workbench.tree.enableStickyScroll": false,
    "workbench.tree.indent": 20
}
# .clang-format
BasedOnStyle: LLVM
AllowShortBlocksOnASingleLine: Always
AllowShortCaseExpressionOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortCompoundRequirementOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
ColumnLimit: 100
IndentWidth: 4

3. clangd(可选)

3.1. 安装扩展

安装 clangd 扩展即可。

其实这时 C/C++ 扩展已经没用了,可以直接卸载掉。

3.2. 配置文件

配置 clangd 后的目录结构应为如下:

.
├── .vscode
│   └── settings.json
├── .clang-format
└── .clangd

需要修改或添加的配置文件内容:

--- settings.json
+++ settings.json
@@ -5 +4,0 @@
-    "C_Cpp.autoAddFileAssociations": false,
# .clangd
CompileFlags:
    Add:
        - -std=c++23
        - -Wall
        - -Wextra
        - -O2
        - -march=native
Diagnostics:
    MissingIncludes: Strict
InlayHints:
    Enabled: false
SemanticTokens:
    DisabledKinds: Operator

另外还有一个问题就是 clangd 可能会被大结构体数组卡到很慢,例如:

std::pair<int, int> hack_clangd[1 << 20];

如果出现这种情况,你可以 Ctrl+Shift+P 然后输入 >clangd.restart 重启 clangd。

VS Code,启动!