在Linux中配置VScode C++开发环境

安装g++编译器

1
2
sudo apt update
sudo apt install g++

检查是否安装正确

1
g++ --version

如果正确显示了g++的版本,那么表示安装成功 Linux 平台自带了 gcc 和 make 等等编译工具,所以不需要进行额外的配置。

配置编译和调试文件

安装插件WSL 后选择connect to WSL

按快捷键F5 进行编译,会在.vscode里生成launch.json文件,按照下面的表进行配置即可,注意与windows有细微的区别

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
{

    "version": "0.2.0",

    "configurations": [

        {//这个大括号里是我们的‘调试(Debug)’配置

            "name": "Debug", // 配置名称

            "type": "cppdbg", // 配置类型,cppdbg对应cpptools提供的调试功能;可以认为此处只能是cppdbg

            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)

            "program": "${fileDirname}/bin/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径

            "args": [], // 程序调试时传递给程序的命令行参数,这里设为空即可

            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点

            "cwd": "${fileDirname}", // 调试程序时的工作目录,此处为源码文件所在目录

            "environment": [], // 环境变量,这里设为空即可

            "externalConsole": false, // 为true时使用单独的cmd窗口,跳出小黑框;设为false则是用vscode的内置终端,建议用内置终端

            "internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,新手调试用不到

            "MIMode": "gdb", // 指定连接的调试器,gdb是minGW中的调试程序

            "miDebuggerPath": "/usr/bin/gdb", // 指定调试器所在路径,如果你的minGW装在别的地方,则要改成你自己的路径,注意间隔是/

            "preLaunchTask": "C/C++: g++ build active file" // 调试开始前执行的任务,我们在调试前要编译构建。与tasks.json的label相对应,名字要一样

    }]

}

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{

    "version": "2.0.0",

    "tasks": [

        {

            "type": "cppbuild",

            "label": "C/C++: g++ build active file",

            "command": "g++",

            "args": [

                "-fdiagnostics-color=always",

                "-g",

                "${file}",

                "-o",

                "${fileDirname}/bin/${fileBasenameNoExtension}.exe",

                "-std=c++14",

                "-Wall",

                "-static-libgcc",

                //"-fexec-charset=GBK"

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$gcc"

            ],

            "group": "build",

            "detail": "compiler: /usr/bin/g++"

        },

             {//这个大括号里是‘运行(run)’任务,一些设置与上面的构建任务性质相同

             "label": "run",

             "type": "shell",

             "dependsOn": "build", //任务依赖,因为要运行必须先构建,所以执行这个任务前必须先执行build任务,

             "command": "${fileDirname}/bin/${fileBasenameNoExtension}.exe", //执行exe文件,只需要指定这个exe文件在哪里就好

             "group": {

                 "kind": "test", //这一组是‘测试’组,将run任务放在test组里方便我们用快捷键执行

                 "isDefault": true

             },

             "presentation": {

                 "echo": true,

                 "reveal": "always",

                 "focus": true, //这个就设置为true了,运行任务后将焦点聚集到终端,方便进行输入

                 "panel": "new"

             }

         }

    ]

}