300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Ubuntu16.04配置Visual Studio Code C++开发环境

Ubuntu16.04配置Visual Studio Code C++开发环境

时间:2023-09-01 18:17:38

相关推荐

Ubuntu16.04配置Visual Studio Code C++开发环境

Ubuntu16.04配置Visual Studio Code C++开发环境

目录

Ubuntu16.04配置Visual Studio Code C++开发环境

一、安装Visual Studio Code及C++插件

1、Visual Studio Code 安装

2、安装C++插件

二、创建工程并添加配置文件及任务文件

1、创建工程文件夹

2、打开工程文件夹

3、配置launch.json文件

4、任务文件task.json

三、编译运行程序

1、先编译后运行

2、编译运行一步到位

一、安装Visual Studio Code及C++插件

1、Visual Studio Code 安装

前往官网/下载deb软件包,下载成功收双击deb软件包即可通过软件商店安装。

2、安装C++插件

操作如下:

随着时间推移,C++插件版本可能会升级,选择介绍包括关键词为IntelliSense,Debugging,code browsing的插件,一般为下载安装人数最多的插件,如图:

二、创建工程并添加配置文件及任务文件

1、创建工程文件夹

Vscode中添加的C++插件版本无法对单独的xx.cpp文件进行编译,需要基于工程完成对xx.cpp文件的编译,而Vscode是通过文件夹管理工程的,因此首先创建文件夹,这里我创建名为C++ Primer的文件夹。

2、打开工程文件夹

在Vscode中通过File->Open Floder打开步骤1中创建的文件夹:

在文件夹下面创建xx.cpp文件,这一文件的用途是为我们配置相关配置文件提供入口,叫什么都无所谓,这里以main.cpp为例。

注:下面的动图引用了他人动图,动图中显示的实在HELLO文件夹下创建的,这个HELLO文件夹就相当于我们在第一步中创建的C++ PRIMER文件夹

引用来源声明:Ubuntu16.04下配置VScode的C/C++开发环境

创建的main.cpp代码如下:

#include <iostream>using namespace std;int main(){cout << "HELLO WORLD" << endl;return 0;}

3、配置launch.json文件

该文件用于配置执行程序最终生成的可执行文件过程中的相关参数。

如图,在打开main.cpp的情况下,点击左侧的RUN按钮

然后点击蓝色字:create a launch.json file,并在选择环境时选择C++ (GDB/LLDB)

在选择配置时选择默认配置:

则生成默认配置的launch.json文件如下:

更改该配置文件如下:

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) Launch","type": "cppdbg","request": "launch","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}]}]}

参数解析如下:

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /fwlink/?linkid=830387"version": "0.2.0",//该launch.json文件对应的版本号,目前默认为0.2.0"configurations": [{"name": "(gdb) Launch", "type": "cppdbg","request": "launch",//需要运行的是当前打开文件的目录中,名字和当前文件相同,但扩展名为out的程序"program": "${workspaceFolder}/${fileBasenameNoExtension}.out","args": [],"stopAtEntry": false, //选为true则会在打开控制台后停滞,暂时不执行程序"cwd": "${workspaceFolder}",//当前工作路径:当前文件所在的工作空间"environment": [],//true为使用外部工作台,会额外打开一个终端显示程序运行结果,false使用vscode自带的工作台"externalConsole": true, "MIMode": "gdb","preLaunchTask": "build","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]}

4、任务文件task.json

该文件用途是便于在Vscode中直接对代码进行编译和链接,而不用从终端再输入g++ -g xx.cpp等编译链接指令,添加方法如下:

首先在xx.cpp界面,利用快捷键ctrl+shift+p打开命令行,输入Tasks: Run task,会出现如下提示:

选择Task:Run Task,如果看不到该选择,则可以在顶部的方框内输入Run Task 来对该命令进行搜索,然后依次选择:

No task to run found. configure tasks...

Create tasks.json file from template

OthersExample to run an arbitrary external command.

生成默认task.json文件如下:

{// See /fwlink/?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0","tasks": [{"label": "echo","type": "shell","command": "echo Hello"}]}

更改该文件如下:

{// See /fwlink/?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0",//该tasks.json文件采用的版本号,目前默认为2.0.0版本"tasks": [{"label": "build","type": "shell","command": "g++","args": ["-g","${file}","-std=c++11","-o","${fileBasenameNoExtension}.out"],"problemMatcher": ["$gcc"]}]}

文件参数解析如下:

{// See /fwlink/?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0",//该tasks.json文件采用的版本号,目前默认为2.0.0版本"tasks": [{"label": "build",//该task(任务)的名称"type": "shell",//任务执行的是shell命令"command": "g++",//命令为“g++”//g++命令的相关参数"args": ["-g","${file}", //要被编译的文件:${file}为当前文件名,包括拓展名"-std=c++11", //采用c++11标准"-o","${fileBasenameNoExtension}.out"//编译后生成的文件名称://${fileBasenameNoExtension}为当前文件名去掉拓展名],"problemMatcher": [//语法错误扫描类型采用gcc类型,注:g++编译器用gcc类型错误"$gcc"]}]}// 所以以上部分,就是在shell中执行(假设文件名为main.cpp)// g++ -g main.cpp -std=c++11 -o main.out

三、编译运行程序

注:编译生成的可执行文件都在项目文件夹下,即C++ Primer文件夹下。

1、先编译后运行

在xx.cpp界面按快捷键“Ctrl+Shift+B”,选择build进行编译,如图:

如果编译失败,终端会给出提示:

> Executing task: g++ -g /home/zhangman/C++Primer/test/main.cpp -std=c++11 -o /home/zhangman/C++Primer/main.out </home/zhangman/C++Primer/test/main.cpp: In function ‘int main()’:/home/zhangman/C++Primer/test/main.cpp:5:5: error: ‘coout’ was not declared in this scopecoout << "HELLO WORLD" << endl;^The terminal process terminated with exit code: 1Terminal will be reused by tasks, press any key to close it.

没有错误则编译通过:

> Executing task: g++ -g /home/zhangman/C++Primer/test/main.cpp -std=c++11 -o /home/zhangman/C++Primer/main.out <Terminal will be reused by tasks, press any key to close it.

然后点击左上角绿色三角RUN运行:

运行结果会在终端显示:

HELLO WORLD[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-5fs21lad.xwd" 1>"/tmp/Microsoft-MIEngine-Out-9ubhl15o.0ct"

2、编译运行一步到位

方法一:打开xx.cpp文件,直接点击:方法二:打开xx.cpp文件,直接按F5

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。