本文档记录在 Windows 系统上从零配置 VS Code C++ 开发环境的全过程,包括编译、调试和运行。

前置条件

需要的工具

1. MSYS2

MSYS2 是一个 Windows 下的 Linux-like 环境,提供 g++ 编译器和 gdb 调试器。

2. VS Code 扩展

  • C/C++ 扩展:由 Microsoft 提供,支持 IntelliSense、调试等功能
    • 在 VS Code 中搜索安装:ms-vscode.cpptools

安装步骤

步骤 1:安装 MSYS2

  1. 下载 MSYS2 安装包并运行安装程序
  2. 选择安装路径为 C:\msys64
  3. 安装完成后,运行 MSYS2 UCRT64 终端(从开始菜单启动)

步骤 2:更新 MSYS2 并安装工具

在 MSYS2 UCRT64 终端中执行以下命令:

1
2
3
4
5
# 更新包数据库
pacman -Syu

# 安装 g++ 编译器和 gdb 调试器
pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-gdb

步骤 3:安装 VS Code 扩展

  1. 打开 VS Code
  2. Ctrl+Shift+X 打开扩展面板
  3. 搜索 C/C++ 并安装 Microsoft 的 C/C++ 扩展

VS Code 配置

在你的 C++ 项目根目录(例如 C:\Dev\leetcode)创建 .vscode 文件夹,并添加以下配置文件:

1. c_cpp_properties.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "C:\\msys64\\ucrt64\\bin\\gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}

2. settings.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
{
"C_Cpp.default.compilerPath": "c:\\msys64\\ucrt64\\bin\\gcc.exe",
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"terminal.external.windowsExec": "C:\\Windows\\System32\\cmd.exe"
}

3. tasks.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
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${workspaceFolder}\\build\\Debug\\outDebug.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Compile current C++ file to build/Debug/outDebug.exe"
},
{
"type": "shell",
"label": "Run C++ Program",
"command": "cmd.exe",
"args": [
"/c",
"start \"\" cmd.exe /k \"${workspaceFolder}\\build\\Debug\\outDebug.exe\""
],
"dependsOn": [
"C/C++: g++.exe build active file"
],
"options": {
"shell": {
"executable": "C:\\Windows\\System32\\cmd.exe"
}
},
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared"
},
"problemMatcher": []
}
]
}

4. 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
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "c:/Dev/leetcode",
"program": "${workspaceFolder}/build/Debug/outDebug.exe",
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

5. keybindings.json(全局配置)

在 VS Code 用户设置中添加快捷键绑定(Ctrl+Shift+P -> Preferences: Open Keyboard Shortcuts (JSON)):

1
2
3
4
5
6
7
8
9
10
[
{
"key": "f6",
"command": "workbench.action.tasks.runTask",
"args": {
"task": "Run C++ Program"
},
"when": "editorTextFocus"
}
]

使用方法

  1. 编译:按 Ctrl+Shift+B 或打开命令面板运行 “Tasks: Run Build Task”
  2. 调试:按 F5,会启动 gdb 调试器
  3. 运行:按 F6,会打开 cmd 黑框窗口运行程序

常见问题

1. 找不到 g++ 或 gdb

  • 确保 MSYS2 已正确安装并更新
  • 检查 PATH 环境变量是否包含 C:\msys64\ucrt64\bin

2. 编译失败

  • 检查代码语法错误
  • 确保头文件路径正确

3. 调试失败

  • 确保已编译生成 build/Debug/outDebug.exe
  • 检查 gdb 路径是否正确

4. 黑框不弹出

  • 重启 VS Code
  • 检查 terminal.external.windowsExec 设置

版本信息

  • MSYS2: 最新版本
  • g++: 15.2.0
  • gdb: 17.1
  • VS Code: 最新版本
  • C/C++ 扩展: 最新版本

参考资料