如何在 Windows 11 Pro 的 vscode 上使用 MSYS2 和 cph 插件打算法竞赛
一、安装与验证:MSYS2 + C++23 编译器
-
装 MSYS2
到官网下载安装(默认到C:\msys64就好)。MSYS2 是在 Windows 上管理 MinGW-w64/GCC/Clang 的发行版。(msys2.org) -
装 64 位 UCRT64 工具链(推荐)
打开开始菜单里的 MSYS2 UCRT64 终端,执行(会自动包含 g++, gdb 等):
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

直接按回车即可(默认安装全部 13 个包)
这是 VS Code 官方文档推荐的做法;该工具链组里包含 mingw-w64-ucrt-x86_64-gcc/g++/gdb/...。(Visual Studio Code, MSYS2 Packages)
说明:MSYS2 有多套环境(
ucrt64/clang64等)。一般做竞赛推荐 UCRT64 + GCC,即稳定、兼容性好;也可以选 UCRT64 + Clang(pacman -S mingw-w64-ucrt-x86_64-clang)。(msys2.org, MSYS2 Packages)
-
把编译器加入 PATH(给 VS Code/终端用)
把C:\msys64\ucrt64\bin加到你用户的 PATH(“编辑环境变量”界面中新建这一条目)。这是 VS Code 官方教程的建议路径。(Visual Studio Code) -
验证(新开一个终端/PowerShell):
g++ --version
gdb --version
能看到版本号就 OK(当前 MSYS2 提供的 GCC 较新,支持 -std=c++23/gnu++23 等)。(Visual Studio Code, MSYS2 Packages, GCC)
二、装 VS Code 扩展
- C/C++ by Microsoft(IntelliSense/调试)→ VS Code 扩展市场搜索 “C++” 安装即可。(Visual Studio Code)
- CPH – Competitive Programming Helper(测试样例/自动评测/对接比赛站点)→ 搜索 “competitive programming helper”。(Visual Studio Marketplace)
- Competitive Companion(浏览器扩展,用来从 Codeforces/AtCoder 等页面把题目和样例一键发到 VS Code/CPH)——安装后在题面点击绿色的 “+” 即可推送到 VS Code。(Chrome Web Store)
CPH 的快捷键:Ctrl + Alt + B 运行当前题目的样例评测。(Visual Studio Marketplace, GitHub)
三、让 VS Code 识别 C++23 与 MSYS2 的编译器
这一步只影响 智能提示/跳转/调试配置,不影响 CPH 自己那套“编译并跑样例”的逻辑。
- 在 VS Code 里:Ctrl+Shift+P → C/C++: Edit Configurations (UI),设置:
- Compiler path:
C:\msys64\ucrt64\bin\g++.exe(或clang++.exe) - C++ standard:
c++23
保存后会生成.vscode/c_cpp_properties.json,类似:
{
"configurations": [
{
"name": "MSYS2 UCRT64",
"compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
"intelliSenseMode": "windows-gcc-x64",
"cppStandard": "c++23",
"includePath": ["${workspaceFolder}/**"]
}
],
"version": 4
}
(intelliSenseMode 如用 Clang,可选 windows-clang-x64。)(Visual Studio Code)
- 可选:把 MSYS2 UCRT64 作为 VS Code 的集成终端
设置里添加一个终端配置,使用官方给出的msys2_shell.cmd启动方式:
{
"terminal.integrated.profiles.windows": {
"MSYS2 UCRT": {
"path": "cmd.exe",
"args": ["/c", "C:\\msys64\\msys2_shell.cmd -defterm -here -no-start -ucrt64"]
}
}
}
这样在 VS Code 里开终端就直接是 MSYS2 的 UCRT64 环境。(msys2.org)
四、让 CPH 使用 MSYS2 的 C++23 编译器
CPH 自带“按键即编译+跑样例”的能力,我们要做的是把它的 C++ 编译器指向 MSYS2 的 g++/clang++,并补上 -std=gnu++23(或
-std=c++23)等参数。
-
打开 设置(Ctrl+,),搜索 “cph”。
找到 “Cph > Language > Cpp > Command”(名称可能略有变化,但意思是“C++ 的编译器命令”),把它改成你的编译器绝对路径,例如:
C:\msys64\ucrt64\bin\g++.exe。该设置项官方 issue 中有说明。(GitHub) -
在 CPH 的 C++ 相关设置里,把 编译参数(可能叫 Args/Flags/Options 之类)加入常用参数,例如:
-std=gnu++23 -O2 -pipe -Wall -Wextra -Wshadow
GCC/Clang 的 -std=c++23/-std=gnu++23 用于启用 C++23(gnu++23 允许 GNU 拓展,竞赛常用)。(GCC)
- 用法
- 从题面页面点 Competitive Companion 的绿色 “+” 把题目发送到 VS Code/CPH;
- 回到 VS Code,CPH 会生成文件并预置样例;
- 按 Ctrl+Alt+B 让 CPH 用刚才配置的编译器编译并对拍样例。(Chrome Web Store, Visual Studio Marketplace)
五、(可选)配置一键编译/调试(不经 CPH)
有时你希望在本地给某个文件直接“构建 + 调试”。可在项目下加两个文件:
.vscode/tasks.json(用 MSYS2 的 g++,C++23,O2):
{
"version": "2.0.0",
"tasks": [
{
"label": "g++ build (C++23, O2)",
"type": "shell",
"command": "C:/msys64/ucrt64/bin/g++.exe",
"args": [
"-std=gnu++23", "-O2", "-pipe", "-Wall", "-Wextra", "-Wshadow",
"${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe"
],
"group": { "kind": "build", "isDefault": true },
"problemMatcher": ["$gcc"]
}
]
}
(tasks 的写法见 VS Code 文档。)(Visual Studio Code)
.vscode/launch.json(GDB 调试当前文件):
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug current file (GDB)",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"cwd": "${fileDirname}",
"stopAtEntry": false,
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/ucrt64/bin/gdb.exe",
"preLaunchTask": "g++ build (C++23, O2)"
}
]
}
(如果调试提示 miDebuggerPath 无效,多半是没装 gdb,装上工具链组或单装 mingw-w64-ucrt-x86_64-gdb 即可。)(Visual Studio Code)
六、快速自测
- 新建
A.cpp,写个小程序读入若干行并回显。 - 在 CPH 面板里添加两个样例(或用 Competitive Companion 从题面导入)。
- 按 Ctrl+Alt+B,看编译与判题输出是否正常(会显示 AC/WA/TLE 等)。(Visual Studio Marketplace)
七、常见坑 & 提示
g++找不到:确认C:\msys64\ucrt64\bin已加入 PATH,或在 CPH 里把 C++ Command 改成绝对路径(…\g++.exe)。(Visual Studio Code, GitHub)- 用哪个 MSYS2 环境? 做算法竞赛优先 UCRT64;它比旧的 msvcrt 环境更现代,兼容性更好。(msys2.org)
- 要不要 Clang? UCRT64 下的 Clang/
clang++也能直接用,装包mingw-w64-ucrt-x86_64-clang即可;把 CPH 的命令改为clang++.exe就能切换到 Clang。(MSYS2 Packages) - 标准与选项:
-std=c++23或-std=gnu++23均可;竞赛通常用gnu++方言(允许 GNU 拓展)。可按个人习惯精简/调整警告和优化参数。(GCC) - 一键从题面导入:浏览器安装 Competitive Companion 扩展(Chrome/Edge/Firefox 都有),在题目页点绿色 “+”。(Chrome Web Store)

浙公网安备 33010602011771号