您的位置:首页 > 编程语言 > C语言/C++

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

2017-12-02 14:35 1356 查看
上网找了很多例子,发现按照网上的教程配置环境无法成功,后来整合了几个例子,发现可成功运行,开此博客已进行记录。

安装gcc和g++

sudo add-apt-repository ppa:ubuntu-toolchain-r/tes  //添加源
sudo apt-get update //更新
sudo apt-get install gcc    //安装gcc
sudo 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),点击最上面的小齿轮,打开
4000
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并不显示)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: