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

56次阅读
没有评论

共计 2071 个字符,预计需要花费 6 分钟才能阅读完成。

这篇文章主要介绍“ubuntu 下如何安装并配置 vs code 编译 c ++”,在日常操作中,相信很多人在 ubuntu 下如何安装并配置 vs code 编译 c ++ 问题上存在疑惑,丸趣 TV 小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ubuntu 下如何安装并配置 vs code 编译 c ++”的疑惑有所帮助!接下来,请跟着丸趣 TV 小编一起来学习吧!

安装 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 ++,

然后会自动生成 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

 

此时将自动生成 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 运行,程序完美输出。有图为证,哈哈

后记:

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

sudo apt-get install clang

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

到此,关于“ubuntu 下如何安装并配置 vs code 编译 c ++”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注丸趣 TV 网站,丸趣 TV 小编会继续努力为大家带来更多实用的文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-15发表,共计2071字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)