网上关于VS code配置C++的教程很多,但是大部分实属操蛋,要copy什么tasks.json文件、launch.json文件啥的。因为以前不想看官网的英文教程,所以以前配置过程都是粘贴网上的配置好的文件。但是想想越觉得不对,踏马配个环境这么复杂,要不要这么辣鸡。索性直接去看官网教程,结果发现官网教程即简洁又易懂(汗)。。。。。踏马弟弟教程真多。
不吐槽了,为了造福网友,下面是我根据官网写了一份简易教程。
(官网传送门:https://code.visualstudio.com/docs)
一:下载 VS code
自行下载
https://code.visualstudio.com/
二:下载C++编译环境
我用的是windows下MINGW,threads-win32, seh异常处理模型。
下载后配置环境变量。
三、下载VS code 中C++插件

下载这个。(快捷键:ctrl+shift+E打开拓展)
四、配置C++运行环境
其实上面主要做了两件事:
- 下载MINGW,配置环境变量
- 安装C++插件
- 在确保完成上面的事情后,打开自己的工作目录。随便写一个C++文件。
#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;
}

- 生成task.json文件
选择:Terminal > Configure Default Build Task.


然后就自动生成task.json文件(不用粘贴网上的代码)。

每个字段的意思可去官网查看。
- 生成launch.json
选择:Run > Add Configuration


选择第一个后生成如下文件:

五、测试
现在测试一下下面代码:
#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;
}
选择: Run > Start Debugging(或者按F5)
PS:按一下F5后,代码运行后就退出怎么办?

方法一:设置一个断点。
方法二:修改launch.json文件中“stopAtEntry”字段。

自动生成时默认时false,改成true就能实现运行代码时自动进入调试。
网友评论