Ubuntu下安装并配置VS Code编译C++的方法
ubuntu下安装并配置vs code编译c++
安装vs code
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
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
</td>
</tr></tbody></table>
然后按a直接默认同意就可以。
安装插件
打开vs code后,按crtl + shift + p调出命令行,然后搜索c++,安装微软自己开发的那个。
同样可以安装c++ intellisense插件,用于自动补全代码。
配置launch.json和tasks.json
注意vs code只能打开源码所在的文件夹,而不是直接打开源码文件,否则下面将无法进行!
打开源码所在文件夹后,在该文件夹中打开源码。按f5键,选择c++,
然后会自动生成launch.json文件,下面只需要修改两个地方
将
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"program": "enter program name, for example \${workspaceroot}/a.out",
</td>
</tr></tbody></table>
改为
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"program": "${workspaceroot}/a.out",
</td>
</tr></tbody></table>
将
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"cwd": "\${workspaceroot}",
</td>
</tr></tbody></table>
改为
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"cwd": "${workspaceroot}",
</td>
</tr></tbody></table>
完整的launch.json
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
{
"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
}
]
}
]
}
</td>
</tr></tbody></table>
然后,调出命令行,输入task runner,选择others
此时将自动生成tasks.json
将其中的
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"command": "echo",
</td>
</tr></tbody></table>
改为
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"command": "g++",
</td>
</tr></tbody></table>
将
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"args": ["hello world"],
</td>
</tr></tbody></table>
改为
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
"args": ["-g","${workspaceroot}/main.cpp"],
</td>
</tr></tbody></table>
注意这里的main.cpp要和你当前路径的源码名称一致。
完整的tasks.json
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
{
// 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"
}
</td>
</tr></tbody></table>
运行测试
随便编写个代码
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
#include
using namespace std;
int main()
{
cout<<"hello vs code"<<endl;
return 0;
}
</td>
</tr></tbody></table>
按crtl + shift + b构建,按f5运行,发现终端一闪而过,什么都没有输出。于是考虑windows下的办法。
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
#include
#include<stdlib.h>
using namespace std;
int main()
{
cout<<"hello vs code"<<endl;
system("pause");
return 0;
}
</td>
</tr></tbody></table>
同样并没有卵用。那就换一种方式。
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
#include
#include<stdio.h>
using namespace std;
int main()
{
cout<<"hello vs code"<<endl;
getchar();
return 0;
}
</td>
</tr></tbody></table>
按crtl + shift + b构建,按f5运行,程序完美输出。有图为证,哈哈
后记:
期间在终端里执行了以下操作
<span>?</span>
<table cellpadding="0" cellspacing="0"><tbody><tr>
<td>
sudo apt-get install clang
</td>
</tr></tbody></table>
如果提示clang有错可以运行该命令,安装clang。
那么问题来了,是不是换个文件夹每次写个代码都得配置lauch.json和task.json文件呢?或者将.vscode文件夹复制到当前文件夹下?这样岂不是很麻烦,细思极恐
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/qq_22186119/article/details/73618062