1. 下载VScode编辑器

进入官网:https://code.visualstudio.com/ 下载VSCode编辑器

vscode下载

2.安装VSCode

这一步应该不用我多说了,没有特殊需求一路下一步即可。

vscode安装

3.下载编译器MinGW

mingw下载

选择下载x86_64-win32.seh,并选择一个目录进行解压(建议是C盘,当然其他盘问题也不大)

4.配置环境变量

配置环境变量

5.安装插件

我们需要安装中文包和C++插件,如图Snipaste_2023-05-27_21-59-14

6.运行代码

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World!\n");
system("pause");
return 0;
}

Snipaste_2023-05-27_22-20-01

选择两次第一个(第二个忘记截图了qwq)

Snipaste_2023-05-27_22-22-27

控制台正常输出即为配置成功

7.配置文件修改

该部分解决的是:

1.中文乱码问题

2.启用外部控制台

3.部分出现C++文件无法编译问题

.vscode文件夹下面launch.json参考以下,"miDebuggerPath"中请填写你的MinGW路径

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
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
},
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}

在“tasks.json”中,"command"中也是填写你的MinGW路径

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
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "D:\\MinGW\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "调试器生成的任务。"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

1."-fexec-charset=GBK"这个配置项用于解决中文乱码

2. "externalConsole": true这个配置项用于使用外部控制台

3.添加g++.exe相关配置项用于解决部分版本c++文件编译失败问题

(这边建议需要用到该配置项的同学,vscode将c和c++分开两个工作区,c使用gcc.exe,c++使用g++.exe)

8.注意事项

  • 源代码文件夹含有中文路径,将会无法编译程序。
  • Windows用户名使用中文,可能无法运行。