【vscode】配置c++环境

前言

vscode本身并不能编译运行c++项目,但是我们可以通过安装一些插件并进行一些配置来使其适合开发小型的c++项目。

安装c++相关插件

在扩展侧栏搜索c++,安装如下插件。

C/C++

C++ Intellisense

Include Autocomplete

安装mingw

mingw推荐从source_forge网站下载,在mingw全版本下载下载mingw,建议版本x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z,安装完成后配置其环境变量即可。

在命令行输入gcc -v,出现类似如下结果说明安装成功。

测试mingw安装完成

配置task.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
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build",
"command": "g++",
"args": ["-g", "${file}", "-o", "${workspaceFolder}/${fileBasenameNoExtension}.exe"],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
}
]
}

注意,task命令为一个数组,可以配置多个task,可以使用dependsOn字段指定执行当前任务前需要执行的命令。

task命令的具体配置参数与含义请参考官方文档

配置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
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "f:\\ide\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}

配置c_cpp_properties.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
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"f:/ide/mingw64/include/**",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"f:/ide/mingw64/include/**",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"f:/ide/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
},
"compilerPath": "f:/ide/mingw64/bin/g++.exe"
}
],
"version": 4
}

配置完成后按F5即可运行。设置断点,在调试侧栏中可观察变量与堆栈。