简述

  • MinGW on Windows
  • VSCode

配置 VSCode + MinGW

选用 VSCode 纯粹是因为用习惯了,用 Dev-C++ 之流总觉得外观啊、自动提示啊什么的不太对劲。

至于为什么用 MinGW,其实一开始是用 WSL 的,但后面发现交作业得附一个可执行文件,助教一般用的是 Windows,交个 Linux 下的可执行文件似乎有些捞,所以干脆把开发环境切换回 Windows。

简要步骤

按照 VSCode 的官方教程 即可。只有几点需要注意:

  1. MinGW 的在线安装器实在是太慢了,建议去 SourceForge 直接下载 zip 包(本例中下载的是 MinGW-W64 GCC-8.1.0: x86_64-posix-seh),然后解压到 C:\Program Files\mingw-w64\ 下,再把 mingw64\bin 文件夹添加到环境变量 PATH 中(本例是 C:\Program Files\mingw-w64\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\mingw64\bin

VSCode 调试配置

tasks.json

启用连接编译

注意 -g 的下一行。

1
2
3
4
5
6
7
8
...
"args": [
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
]
...

编译出来的 exe 显示中文乱码

参考了 MinGW 控制台中文乱码 - 博客园

1
2
3
4
5
6
7
8
9
...
"args": [
"-g",
"-fexec-charset=GBK", // 处理 MinGW 中文编码问题
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}\\PROJECT_NAME.exe"
]
...

两个文件的最终成品

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
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
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
},
{
"name": "g++.exe - 生成可发布的 exe 文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build executable for release"
}
]
}

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
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "C/C++: g++.exe build executable for release",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"-fexec-charset=GBK", // 处理 MinGW 中文编码问题
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}\\PROJECT_NAME-v.exe" // v 后面填版本号
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}