共計 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 小編會繼續努力為大家帶來更多實用的文章!