這篇文章主要介紹“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++,

然后會自動生成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++”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。