温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Ubuntu下怎么安装并配置VS Code编译C++

发布时间:2022-04-13 16:25:45 阅读:374 作者:iii 栏目:编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍“Ubuntu下怎么安装并配置VS Code编译C++”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Ubuntu下怎么安装并配置VS Code编译C++”文章能帮助大家解决问题。

ubuntu下安装并配置vs code编译c++

安装vs code

sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make
sudo apt-get update
sudo apt-get install ubuntu-make
sudo umake web visual-studio-code

然后按a直接默认同意就可以。

安装插件

打开vs code后,按crtl + shift + p调出命令行,然后搜索c++,安装微软自己开发的那个。

同样可以安装c++ intellisense插件,用于自动补全代码。

配置launch.json和tasks.json

注意vs code只能打开源码所在的文件夹,而不是直接打开源码文件,否则下面将无法进行!

打开源码所在文件夹后,在该文件夹中打开源码。按f5键,选择c++,

Ubuntu下怎么安装并配置VS Code编译C++

然后会自动生成launch.json文件,下面只需要修改两个地方

"program": "enter program name, for example \${workspaceroot}/a.out",

改为

"program""${workspaceroot}/a.out",

"cwd": "\${workspaceroot}",

改为

"cwd""${workspaceroot}",

完整的launch.json

{
  "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
        }
      ]
    }
  ]
}

然后,调出命令行,输入task runner,选择others

Ubuntu下怎么安装并配置VS Code编译C++ 

此时将自动生成tasks.json

将其中的

"command": "echo",

改为

"command": "g++",

"args": ["hello world"],

改为

"args": ["-g","${workspaceroot}/main.cpp"],

注意这里的main.cpp要和你当前路径的源码名称一致。

完整的tasks.json

{
  // see https://go.microsoft.com/fwlink/?linkid=733558
  // for the documentation about the tasks.json format
  "version""0.1.0",
  "command""g++",
  "isshellcommand"true,
  "args": ["-g","${workspaceroot}/main.cpp"],
  "showoutput""always"
}

运行测试

随便编写个代码

#include<iostream>
using namespace std;

int main()
{
  cout<<"hello vs code"<<endl;
  return 0;
}

按crtl + shift + b构建,按f5运行,发现终端一闪而过,什么都没有输出。于是考虑windows下的办法。

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
  cout<<"hello vs code"<<endl;
  system("pause");
  return 0;
}

同样并没有卵用。那就换一种方式。

#include<iostream>
#include<stdio.h>
using namespace std;

int main()
{
  cout<<"hello vs code"<<endl;
  getchar();
  return 0;
}

按crtl + shift + b构建,按f5运行,程序完美输出。有图为证,哈哈

Ubuntu下怎么安装并配置VS Code编译C++

后记:

期间在终端里执行了以下操作

sudo apt-get install clang

如果提示clang有错可以运行该命令,安装clang。

关于“Ubuntu下怎么安装并配置VS Code编译C++”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://www.xuebuyuan.com/3289526.html

AI

开发者交流群×