1.VS Code下载

渠道很多,我这里官网太慢了,重装后以前的IDM也找不到了,所以选的腾讯软件管家下载:​​https://pc.qq.com/detail/16/detail_22856.html​

2.配置MinGW-w64

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

找到那个x86_64-win32-seh的链接,点击下载:

VS Code 安装及C++环境配置_自动生成

3.配置环境变量

下载后是一个7z压缩包,解压后找到bin文件夹,将其放入Path系统变量:

VS Code 安装及C++环境配置_vscode_02

VS Code 安装及C++环境配置_vscode_03

4.配置C++环境

新建cpp文件:

#include <iostream>
using namespace std;
int main()
{
cout << "Hello Vscode" << endl;
return 0;
}

进入调试界面添加配置环境,选择 C++(GDB/LLDB),再选择 g++.exe,之后会自动生成 launch.json 配置文件:

VS Code 安装及C++环境配置_json_04

VS Code 安装及C++环境配置_g++_05

配置 launch.json 文件:

{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true, //修改此项,让其弹出终端
"MIMode": "gdb",
"miDebuggerPath": "D:\\2Software\\mingw64\\bin\\gdb.exe", //换成你的安装路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++" //修改此项
}
]
}

返回.cpp文件,按F5进行调试,会弹出找不到任务"task g++",选择 “配置任务”,会自动生成 tasks.json 文件。

配置 tasks.json 文件:

{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++", //修改此项
"command": "D:\\2Software\\mingw64\\bin\\g++.exe", // 换成你的安装路径
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\2Software\\mingw64\\bin" // 换成你的安装路径
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}

然后运行代码,即可成功运行:

VS Code 安装及C++环境配置_c++_06

设置格式化

打开设置,搜索:

C_Cpp: Clang_format_style

将框中内容改为:

{ BasedOnStyle: LLVM, IndentWidth: 4 }

可将代码格式化为更紧凑的格式:

VS Code 安装及C++环境配置_vscode_07