VS Code配置C/C++环境

1. MinGW下载与配置

配置好环境变量之后,用g++ --version来验证是否成功安装

2. .vscode下4个json文件配置

在.vscode文件夹下创建四个文件,文件名如下:

1
2
3
4
c_cpp_properties.json
launch.json
settings.json
tasks.json

image-20241203194528450

c_cpp_properties.json

注意事项:

​ 1)windowsSdkVersion的查询方法: Win+R组合键打开运行对话框,输入cmd,在终端中输入VER。经查询,本人计算机的windowsSdkVersion为10.0.26100.2454

​ 2)compilerPath需要配置成g++的安装路径。本人的安装路径为C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"configurations": [
{
"name": "Win64",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.26100.2454", //改成自己的
"compilerPath": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/g++.exe", //改成自己的路径
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}

launch.json

注意事项:

​ miDebuggerPath需要配置成gdb的安装路径。本人的安装路径为C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gdb.exe

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
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gdb.exe", //改成自己的路径
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

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
62
63
64
65
{
"files.associations": {
"*.py": "python",
"iostream": "cpp",
"*.tcc": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"ostream": "cpp",
"new": "cpp",
"typeinfo": "cpp",
"deque": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"fstream": "cpp",
"sstream": "cpp",
"map": "c",
"stdio.h": "c",
"algorithm": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"ios": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"memory": "cpp",
"random": "cpp",
"set": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"stdlib.h": "c",
"string.h": "c"
},
"editor.suggest.snippetsPreventQuickSuggestions": false,
"aiXcoder.showTrayIcon": true
}

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
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\g++.exe", //改成自己的路径
"args": [
"-std=c++11", //记得加
"-stdlib=libc++", //记得加
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

3. VScode编写C++之解决cin输入问题

找到左下角齿轮,选择设置

image-20241203195802521

输入run in terminal,将下图的地方打钩

image-20241203195900353

4. 在vscode中添加c++11标准

  1. 点击文件-首选项-设置

  2. 设置中搜code-runner,左侧扩展中找到Run Code configuration,右侧找到Code-runner:Executor Map,打开该项下的在setting.json中编辑

image-20241203200424607
  1. 在图示位置添加-std=c++11,保存
image-20241203200624018
  1. 找到vscode配置c++环境时的task.json,并在图示位置添加-std=c++11,这里刚刚已经配置过了
image-20241203200753026

5. vscode隐藏.class .exe等不必要的文件

  1. 打开vscode并且依次打开文件=>首选项==>设置
  2. 输入files.exclude
  3. 添加自己想要隐藏的文件类型
image-20241203201007956

参考博客:

[1] Visual Studio Code(VS Code)配置C/C++环境_vscode-CSDN博客

[2] 一句话 | VScode编写C++之解决cin输入问题_vscode cin怎么输入-CSDN博客

[3] 【c/c++】在vscode中添加c++11标准(利用code-runner扩展)_vscode c++11-CSDN博客

[4] vscode隐藏.class .exe等不必要的文件_vscode隐藏exe-CSDN博客