VSCode配置cpp环境
下载 VSCode
链接: VSCode 一直下一步
安装 C/CPP 扩展
汉化扩展,可不装
重启VSCode
新建一个文件夹,在文件夹里面新建一个 *.cpp 文件,随便写点代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
安装 MinGW 编译器
链接: MinGW 往下滑,下载最新的 x86_64-posix-sjlj,解压即可,路径不要有中文、空格和特殊字符
配置环境变量
验证是否安装成功
配置 *.json 文件
- tasks.json (build instructions)
- launch.json (debugger settings)
- c_cpp_properties.json (compiler path and IntelliSense settings)
用 VSCode 打开某个文件夹,该文件夹即成为工作区。工作区还需建立一个 .vscode 文件夹,里面存放上述三个 json 文件
- 创建 tasks.json(快捷键 ctrl + shift + p)
注:编译器路径视安装位置而定
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\Application\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\Application\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: D:\\Application\\mingw64\\bin\\g++.exe"
}
]
}
- 创建 launch.json(按F5)
"console": "externalTerminal",
// 改为以下,true 表示从外部显示控制台
"externalConsole": true,
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "D:\\Application\\mingw64\\bin",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Application\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
- 创建 c_cpp_properties.json(快捷键 ctrl + shift + p)
完结撒花:理论上经过上述操作,便可编译、调试和运行 cpp 代码啦
参考教程:
https://code.visualstudio.com/docs/languages/cpp
https://code.visualstudio.com/docs/cpp/config-msvc