VSCode的C++环境基础配置

本文最后更新于:2 年前

其实这篇博客写的已经有点久了,当时是参考的某几个知乎文档,不过现在2021年还是能用的,至于有没有更好的方法那就不知道了,懒得再去找了。话说这是不是就是学校老师还在用一些古早软件的原因?

声明

本文档的一切内容均为本地测试结果,受限于本人知识与能力,仅供参考,如因参照本文档操作而发生任何问题,无论是否严格参照本文档操作,请恕本人概不负责。

文档中的任何观点受限于本人知识、能力及眼界,不保证理智,公正,客观。如本文档中观点与您相左,以您的意见为准。

GCC编译器

官方下载入口

https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

截至2019/6/3最新包:https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/sjlj/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z/download

微云备份下载

截至2019/6/3最新包:

链接:https://share.weiyun.com/5OM2h0x 密码:3nufthr4e

百度云备份下载

截至2019/6/3最新包:

链接: https://pan.baidu.com/s/1NDehXw_FCU2U0lWmrfNfjg 提取码: e1fn

使用说明

下载之后解压,将其中的mingw64文件夹移动到C:\Program Files目录下,将C:\Program Files\mingw64\bin路径添加到系统环境变量Path中。

vscode C++插件

在扩展(左上竖着并排的五个图标第五个)中搜索C++,一般第一个就是

配置tasks.json文件

在目录下新建 .vscode 文件夹,在.vscode文件夹中新建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": [
{
"label": "build",
"type": "process",
"command": "g++",
"args": [
"-g",
"-Wall",
"-std=c++14",
"-lm",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.o"
],
"presentation": {
"echo": false,
"reveal": "silent",
"showReuseMessage": false
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
},
"windows": {
"args": [
"-g",
"-Wall",
"-std=c++14",
"-lm",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
]
}
},
{
"label": "run",
"type": "shell",
"dependsOn": "build",
"command": "${fileDirname}/${fileBasenameNoExtension}.o",
"presentation": {
"focus": true
},
"group": {
"kind": "test",
"isDefault": true
},
"windows": {
"command": "${fileDirname}/${fileBasenameNoExtension}.exe"
}
}
]
}

配置一键运行

点击左下角齿轮图标,选择键盘快捷方式(keyboard shortcuts),搜索tasks,找到Tasks: Run Test Tasks,绑定为喜欢的键(我F10)。

配置调试

在.vscode文件夹中新建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
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.o",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"windows": {
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"miDebuggerPath": "C:/Program Files/mingw64/bin/gdb.exe",
},
"osx": {
"externalConsole": true
},
}
]
}

然后就可以按F5调试了,不需要绑定快捷键,另外如果要使用调试功能,目录名和文件名都不能有中文,否则报错。


VSCode的C++环境基础配置
https://www.jingshan256.com/vscode_cpp/
作者
origincat
发布于
2019年10月7日
许可协议