1.原理
简单来说,VS Code其实是一个文本编辑器,通过配置各种插件使其拥有丰富的功能。
为什么vscode可以用作例如c/c++等语言的开发环境呢?参考
- vscode集成了终端系统,可以在其内部调用命令行终端,而命令行终端可以完成将代码变成程序这个任务。(这里为简洁易懂,有部分不严谨,但是是对的。)
- vscode可以设置一些选项,使得能在上条中提到的命令行终端中执行一套命令,调用语言编译器对代码进行编译等,变成程序。
- vscode可以装一些对应语言的插件,使得开发环境更加完善。
2.下载vscode
下载链接:官网
安装路径不要带中文和空格

3.配置编译器
目前主流的C/C++编译器,Linux平台有 gcc
,Windows平台有 msvc
,MacOS平台有 clang
,但我们选用 gcc
作为编译器。
我们需要用到 MinGW-w64 ,全名Minimalist GNU for Windows,是Linux平台上的一套开发工具,移植到Windows平台。这之中,就有我们需要的gcc。
3.1 下载MinGW-w64
GitHub下载链接,建议下载ucrt版本(universal c++ runtime),其中posix和win32版本区别不大,随便下载哪个都可以。

3.2 安装
建议将压缩包解压到C盘以外,全路径不包含中文和空格。
配置环境变量,将mingw64/bin路径添加到path环境变量中,win+r进入cmd中断输入gcc --version
后显示出版本信息则说明这一步配置成功。
4.配置VS Code
4.1 扩展下载
c/c++:提供了丰富的对C/C++语言的支持,包括单步调试,头文件寻找等等。
Better C++ Syntax:C++语法高亮。
4.2 配置C++环境
vscode打开一个空文件夹,用来写之后的所有c++文件。
可以先创建一个helloworld.cpp文件用于后续配置测试:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
4.2.1 配置编译器
ctrl+shift+p调出命令面板,输入c/c++,选择Edit Configurations(UI)
进入配置,配置两个选项:
1.Compiler path
:选择g++.exe的文件路径(g++专用于编译c++文件,而gcc可以编译c、c++等多种语言文件,如果只需要c++则选择g++.exe更高效)
2.IntelliSense mode
:选择window-gcc-x64即可


配置完成后,侧边栏多了一个.vscode文件夹,里面有一个文件c_cpp_properties.json
,说明配置成功,文件内容如下:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/env/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}

4.2.2 配置构建任务task.json
需要创建task.json告诉VS Code如何构建(编译)程序,该任务将调用g++编译器基于源代码创建可执行文件。
ctrl+shift+p调出命令面板,选择Tasks:Configure Default Build Task
:

再选择C/C++:g++.exe build active file
,此时会出现一个名为task.json的配置文件,内容如下:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",//任务的名字,可以自己设置
"command": "D:/env/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:/env/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: D:/env/mingw64/bin/g++.exe"
}
]
}

4.2.3 配置调试设置 launch.json

进入debugging,选择C++(GDB/LLDB),会生成一个launch.json文件,内容如下:
{
// 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": []
}
我们需要配置configuration中的内容,可以直接点击Add Configuration
按钮,配置结果如下:
注意preLaunchTask
中的字符串要和task.json中的label
内容完全一致。

{
// 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",
"preLaunchTask": "C/C++: g++.exe build active file",//调试前执行的任务,就是之前配置的tasks.json中的label字段
"type": "cppdbg",//配置类型,只能为cppdbg
"request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",//调试程序的路径名称
"args": [],//调试传递参数
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,//true显示外置的控制台窗口,false显示内置终端
"MIMode": "gdb",
"miDebuggerPath": "D:\\env\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
5.测试
运行helloworld.cpp,控制台输出结果
随便打一个断点,f5进入调试模式

配置成功。