跳到正文

个人记录

如何在 Windows 上配置编辑器 VSCodium (MSYS2/CLANG64)

hly1204约 10 分钟
原文

这是 2026年3月17日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年1月10日
收录
2026年3月17日
更新
2026年11月15日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

下载和安装 MSYS2

在 MSYS2 下载和安装 MSYS2 到 Windows 中,这是一个比起 MicroSoft Visual Studio 来说更加符合 Linux 用户习惯的为 Windows 编译“原生”二进制程序的开发环境。我们可以从他的介绍里面看到。

为何选择 CLANG64 而不是 UCRT64?

根据 MSYS2 官方文档描述,如果不知道如何选择,你应该选择 UCRT64 作为你的环境来开始,UCRT64 使用了 GNU 的工具链,但是同样的 UCRT64 也提供了 LLVM 工具链(C++ 标准库还是使用的 libstdc++),而 LLVM 工具链可以使用 libc++ 作为其 C++ 标准库的,至于 C 语言的标准库则是微软的 ucrt。于是这里有了一个选择,我们应该用 GCC 编译器还是 Clang 编译器?为了避免选择困难,于是我们干脆选择只有 LLVM 工具链的 CLANG64 好了。

在安装完 MSYS2 之后,我们需要点开 c:\msys64\clang64.exe 并在其中输入

Bash
pacman -Suy

直到其提示没有更新为止,可能需要若干次。然后我们安装

Bash
pacman -S git \
mingw-w64-clang-x86_64-toolchain \
mingw-w64-clang-x86_64-cmake \
mingw-w64-clang-x86_64-ninja \
mingw-w64-clang-x86_64-zlib \
mingw-w64-clang-x86_64-zstd \
mingw-w64-clang-x86_64-brotli \
mingw-w64-clang-x86_64-7zip

这些常见的库。

下载 VSCodium

在 VSCodium 下载 VSCodium 并安装。

配置 VSCodium

打开 VSCodium 然后选择主题为 Light Modern

Light Modern

然后我们进入扩展,安装 clangd

clangd

注意选择 llvm-vs-code-extensions 为发布者的,不要安装到错误的版本。

然后安装 llvm-dap

llvm-dap

最后我们点击右上角的布局配置

layout

选择红线标志的配置,这样侧边栏就到右侧了,快速输入中心也放到了屏幕中心而不是屏幕上方。

然后我们要修改配置

JSON
"editor.cursorBlinking": "phase",
"editor.cursorStyle": "block",
"files.autoSave": "afterDelay"

将指针修改为块状,并且开启自动保存。我还推荐安装 Error Lens 插件

Error Lens

并且配置为

JSON
"errorLens.editorHoverPartsEnabled": {
    "messageEnabled": true
},
"errorLens.statusBarIconsEnabled": true,
"errorLens.enabledDiagnosticLevels": [
    "error",
    "warning",
    "info",
    "hint"
],
"errorLens.messageBackgroundMode": "message",
"errorLens.gutterIconSet": "defaultOutline",
"errorLens.scrollbarHackEnabled": true

而 clangd 的配置为

JSON
"clangd.arguments": [
    "--log=info",
    "--pretty",
    "--compile-commands-dir=${workspaceFolder}",
    "--header-insertion=never",
    "--all-scopes-completion",
    "--clang-tidy",
    "--pch-storage=memory",
    "-j=8"
],
"clangd.path": "C:\\msys64\\clang64\\bin\\clangd.exe",
"clangd.checkUpdates": false,
"clangd.inactiveRegions.useBackgroundHighlight": true

然后我们将字体配置为 Google Sans Code

JSON
"editor.fontFamily": "'Google Sans Code', 'Courier New', monospace"

然后我们新建一个 test 文件夹,并且用 VSCodium 打开,然后开始配置我们的 C/C++ 语法提示等。

先创建一个 compile_flags.txt 文件,并在里面写下

代码
-xc++
-I
.
-std=c++17
-Wall
-Wextra
-Werror
-Wshadow
-Wformat=2

这告诉 clangd 去读取里面的标志来进行语法提示。

然后我们创建一个 C++ 源文件 main.cc 并在里面写下

C++
#include <iostream>

int main() {
    std::cout << "Hello, world" << std::endl;
    return 0;
}

此时我们发现,如果按住 Ctrl 再鼠标点击头文件或者 std::cout 等,clangd 已经可以帮助我们跳转到声明处了。但是小心,此时我们跳转过去可以修改这些文件,这很危险,于是在配置中可以加入

JSON
"files.readonlyInclude": {
    "**/msys64/clang64/include/**": true
}

这样再次跳转后发现文件名上会多出一个锁的标记,告诉我们这是只读模式。

readonly

快速调节字体大小

到键绑定中删除 Zoom In/Out 的 'Ctrl' + '=/-' 的绑定,查找 Increase/Decrease Font Size 的配置,配置成 'Ctrl' + '=/-',这样可以快速改变字体大小。

配置终端

接下来我们想在 VSCodium 的终端中直接来用命令来编译刚刚我们写的 C++ 源文件,需要打开刚刚的 json 格式的配置文件,并写下

JSON
"terminal.integrated.profiles.windows": {
    "CLANG64": {
        "path": "cmd.exe",
        "args": [
            "/C",
            "C:\\msys64\\msys2_shell.cmd -defterm -here -no-start -clang64"
        ],
        "icon": "terminal-bash",
        "color": "terminal.ansiCyan",
        "overrideName": true
    }
}

然后我们用快捷键 'Ctrl' + 'J' 就可以打开,再到右上角选择 + 号打开 CLANG64 的终端即可。这一配置可以在 IDEs and Text Editors - MSYS2 找到。

我们在终端输入

Bash
c++ main.cc -O2 -std=c++17 -o a && ./a

注意这里的 c++ 其实就是 clang++,但是根据 Linux 中的习惯,c++ 是系统默认的 C++ 编译器。

调试

我们打开 Run and Debug 标签,然后创建一个 launch.json 文件,写下

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": [
        {
            "type": "lldb-dap",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "env": {
                "PATH": "C:\\msys64\\clang64\\bin;C:\\msys64\\usr\\bin;${env:PATH}"
            },
            "cwd": "${workspaceFolder}"
        }
    ]
}

当然,我们需要在配置中加上

JSON
"lldb-dap.executable-path": "C:\\msys64\\clang64\\bin\\lldb-dap.exe"

如此,我们就可以加上断点(注意需要加上编译选项 -g 才会生效,否则也可以用内嵌汇编 int3 来实现(这只在 x86-64 上生效))

debug

一些便利的配置

使得退出后下次自动开启上次的终端

JSON
"terminal.integrated.persistentSessionReviveProcess": "onExitAndWindowClose"

自动换行显示,这样不用拖动左右的滚动条

JSON
"editor.wordWrap": "on"

自动插入最后一行空行

JSON
"files.insertFinalNewline": true