服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Ubuntu - Ubuntu下安装并配置VS Code编译C++的方法

Ubuntu下安装并配置VS Code编译C++的方法

2022-02-28 17:06LTQblog Ubuntu

这篇文章主要介绍了Ubuntu下安装并配置VS Code编译C++的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

安装vs code

?
1
2
3
4
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文件,下面只需要修改两个地方

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

改为

?
1
"program": "${workspaceroot}/a.out",

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

改为

?
1
"cwd": "${workspaceroot}",

完整的launch.json

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "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

将其中的

?
1
"command": "echo",

改为

?
1
"command": "g++",

?
1
"args": ["hello world"],

改为

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

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

完整的tasks.json

?
1
2
3
4
5
6
7
8
9
{
  // 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"
}

运行测试

随便编写个代码

?
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;
 
int main()
{
  cout<<"hello vs code"<<endl;
  return 0;
}

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

?
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<stdlib.h>
using namespace std;
 
int main()
{
  cout<<"hello vs code"<<endl;
  system("pause");
  return 0;
}

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

?
1
2
3
4
5
6
7
8
9
10
#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++的方法

后记:

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

?
1
sudo apt-get install clang

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

那么问题来了,是不是换个文件夹每次写个代码都得配置lauch.json和task.json文件呢?或者将.vscode文件夹复制到当前文件夹下?这样岂不是很麻烦,细思极恐

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/qq_22186119/article/details/73618062

延伸 · 阅读

精彩推荐
  • UbuntuUbuntu安装和卸载CUDA和CUDNN的实现

    Ubuntu安装和卸载CUDA和CUDNN的实现

    这篇文章主要介绍了Ubuntu安装和卸载CUDA和CUDNN的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    夜雨飘零14632020-08-04
  • UbuntuUbuntu Gnome下如何修改应用图标icon?

    Ubuntu Gnome下如何修改应用图标icon?

    最近有些朋友问小编Ubuntu Gnome下如何修改应用图标icon?今天小编要为大家分享的是Ubuntu Gnome下修改应用图标icon的方法;有需要的朋友一起去看看吧...

    服务器之家6412019-06-01
  • UbuntuUbuntu17.10怎么添加日历事项? Ubuntu添加行程提醒的教程

    Ubuntu17.10怎么添加日历事项? Ubuntu添加行程提醒的教程

    Ubuntu17.10怎么添加日历事项?Ubuntu17.10系统中有一个日程管理功能,可以在日历中添加行程提醒,下面我们就来看看Ubuntu添加行程提醒的教程,需要的朋友可...

    服务器之家2812019-06-19
  • UbuntuUbuntu 15.04升级到Ubuntu 15.10的详细教程

    Ubuntu 15.04升级到Ubuntu 15.10的详细教程

    ubuntu15.04怎么升级到ubuntu15.10?又该升级系统了,但是很多人对ubuntu系统很不熟悉,下面我们一起来看看ubuntu15.04升级ubuntu15.10的详细教程,需要的朋友可以...

    服务器之家4562019-07-02
  • UbuntuWSL、WSL2与Ubuntu性能大PK

    WSL、WSL2与Ubuntu性能大PK

    科技媒体 Phoronix 对 Windows 10 May 2020 中 WSL 和 WSL 2 的性能进行了测试,参与测试的发行版为 Ubuntu 20.04 on WSL/WSL2 ,以及 Ubuntu 20.04 LTS,均被安装在除 Windows 之...

    开源中国29412020-06-23
  • UbuntuUbuntu20.04开启root账户的方法步骤

    Ubuntu20.04开启root账户的方法步骤

    这篇文章主要介绍了Ubuntu20.04开启root账户的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    缀梦13002020-08-01
  • Ubuntu如何在ubuntu系统中安装pycharm工具并运行

    如何在ubuntu系统中安装pycharm工具并运行

    在Windows系统中安装pycharm,只需要下载安装包,然后根据指令一步一步操作;而在Linux系统中的Ubuntu中安装pycharm,需要下载安装包,还有安装相关的其他软件...

    百度经验12082019-10-21
  • UbuntuUbuntu下安装Chrome的方法分享

    Ubuntu下安装Chrome的方法分享

    本文给大家分享的是Ubuntu下安装Chrome的方法,安装的过程中发现还是挺麻烦的,就记录下来推荐给大家,有需要的小伙伴可以参考下。...

    Ubuntu教程网3862021-10-25