1. Vscode安装
Visual studio code是微软发布的一个运行于 Mac OS X、Windows和 Linux 之上的,针对于编写现代 Web 和云应用的跨平台源代码编辑器。
安装方式是从VScode官网下载.deb文件,然后双击该文件会打开软件中心进行安装。
安装完毕后即可打开VScode,主界面如下:
2. Vscode环境配置
(1)安装c/c++插件
首先通过左边栏的Extension栏目安装C++插件,操作如下图:
(2) 建立工程
由于VScode是以文件夹的形式管理工程的,因此我们首先新建一个文件夹,我这里取名叫cpp_ws.
然后通过VScode打开此文件夹:
在cpp_ws文件目录下新建cpp_primier1.cpp文件并输入下图中的程序:
(3) 更改配置文件(launch.json)
点击左侧的Debug按钮,选择添加配置(Add configuration),然后选择C+(GDB/LLDB),将自动生成launch.json文件,具体操作如下:
配置后的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": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
//"program": "enter program name, for example ${workspaceFolder}/a.out",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask":"build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
(4) 添加构建(编译、链接等)任务(tasks.json)
首先,利用快捷键ctrl+shift+p打开命令行,输入Tasks: Run task,然后点击:Tasks: Configure Task,然后生成tasks.json文件,详细内容见下图:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g","${file}","-std=c++11","-o","${fileBasenameNoExtension}.out"]
}
]
}
接下来即可通过F5来编译运行代码了。