ubuntu下visual studio code配置C++环境

来源:互联网 发布:非农数据对美元影响 编辑:程序博客网 时间:2024/06/14 01:03

1.安装第三方插件:

在扩展商店里搜索C++,使用微软推荐的插件

2.在电脑中新建一个文件夹(C++只能在文件夹中编译执行,不能单独使用一个文件,也就是说要打开一个文件夹),在文件夹中新建一个.cpp文件

3.visual studio code打开文件夹

4.在.cpp文件中添加c++程序

#include<iostream>#include<cstdio>using namespace std;int main(){    cout<<"Hello World"<<endl;    getchar();    return 0;}
5.在程序中有绿色波浪线处单击,然后点击黄色的小灯泡,添加路径(或者是更新路径)

6.点击F5,配置lunch.json文件,

点击F5之后,提示中选择C++(GDB/LLDB)选项,然后会自动生成lunch.json文件

只需删除"program":"${workspaceRoot}/a.out",行中多余的部分

配置完成如下:

{        "version": "0.2.0",        "configurations": [            {                "name": "(gdb) Launch",                "type": "cppdbg",                "request": "launch",                "program": "${workspaceRoot}/a.out",                "args": [],                "stopAtEntry": false,                "cwd": "${workspaceRoot}",                "environment": [],                "externalConsole": true,                "MIMode": "gdb",                "setupCommands": [                    {                        "description": "Enable pretty-printing for gdb",                        "text": "-enable-pretty-printing",                        "ignoreFailures": true                    }                ]            }        ]    }
7.点击Ctrl+Shift+b,配置tasks.json文件

点击上述组合按键之后,会有提示,此时选择‘配置任务’,选择使用模板创建,然后选择‘others’,然后输入一下代码:

{    // See https://go.microsoft.com/fwlink/?LinkId=733558    // for the documentation about the tasks.json format    "version": "2.0.0",    "tasks": [        {            "taskName": "echo",            "type": "shell",            "command": "/usr/bin/g++",            "args":[                "-g","-Wall","test2.cpp"            ]        }    ]}
要改变此代码中最后一行的文件名

8.点击Ctrl+Shift+b编译文件,生成a.out

9.点击F5运行程序


说明:在源程序中使用getchar();是为了避免程序终端一闪而退

原创粉丝点击