ubuntu 16.04 VSCode 配置C++开发环境

来源:互联网 发布:女明星 瘦 知乎 编辑:程序博客网 时间:2024/05/16 18:34

上网找了很多例子,发现按照网上的教程配置环境无法成功,后来整合了几个例子,发现可成功运行,开此博客已进行记录。
安装gcc和g++

sudo add-apt-repository ppa:ubuntu-toolchain-r/tes  //添加源sudo apt-get update //更新sudo apt-get install gcc    //安装gccsudo apt-get install g++    //安装g++

以上需要键入y以确定

在https://code.visualstudio.com/Download
下载vscode linux版本
这里写图片描述
下载完后,运行终端

cd /home/simon/软件包  //转至vscode安装包文件夹dpkg -i code_1.18.1-1510857349_amd64.deb   //安装已下载的deb包 

这里写图片描述

安装完后打开vscode,安装c++扩展
这里写图片描述

安装完毕后,重载,
新建一个文件夹,命名为main_test,在vs中新建一个main_test.cpp文件
如下:

#include<iostream>#include<stdio.h>using namespace std;int main(void){    int x;    cout<<"请输入x的值:"<<endl;    cin>>x;    cout<<"你输入的x的值为:"<<x;    cout<<endl;    cout<<"Hello,world!"<<endl;    return 0;}

这里写图片描述

打开调试(ctrl+shift+D),点击最上面的小齿轮,打开launch.json
内容如下:

{    // 使用 IntelliSense 了解相关属性。     // 悬停以查看现有属性的描述。    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387    "version": "0.2.0",    "configurations": [        {            "name": "(gdb) Launch",            "type": "cppdbg",            "request": "launch",            "program": "${workspaceFolder}/main_test.out",//编译输出文件的文件名main_test.out,${workspaceFolder}表示vscode所打开的工作目录            "args": [],            "stopAtEntry": false,            "cwd": "${workspaceFolder}",            "environment": [],            "externalConsole": true,                "preLaunchTask":"build",//任务名,tasks.json中会用到,不要忘记,否则编译时会提示为进行配置            "MIMode": "gdb",            "setupCommands": [                {                    "description": "Enable pretty-printing for gdb",                    "text": "-enable-pretty-printing",                    "ignoreFailures": true                }            ]        }    ]}

ctrl+shift+P,在输入框中键入 tasks
这里写图片描述

找到Tasks:Configure Tasks,点击打开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",//任务名,和lanuch.json中的"preLaunchTask":"build"一致            "type": "shell",            "command": "g++",            "args":["-g","${workspaceRoot}/main_test.cpp","-o","main_test.out"],//要编译的文件mian_test.cpp,${workspaceRoot}表示vscode所打开的工作目录            "problemMatcher":            {                "owner":"cpp",                "fileLocation":["relative","${workspaceRoot}"],                "pattern":                {                    "regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",                    "file": 1,                    "line":2,                    "column":3,                    "severity": 4,                    "location": 2,                    "message": 5                }            }        }    ]}

最后
ctrl+shift+B进行编译,编译完后生成一个main_test.out文件,
同事出现如下提示
这里写图片描述
选择要运行的任务,点build
出现如下情况:
这里写图片描述

在终端输入./main_test.out(下图中的main_test,下划线在vscode中的bash并不显示)
这里写图片描述

原创粉丝点击