iT邦幫忙

1

使用Visual studio code (vscode) 一鍵執行編譯C++ compiler 超簡單

不明 2021-07-12 21:59:4632541 瀏覽

**1.安裝Visual Studio code **
有安裝的可跳過這一步
官網連結: http://code.visualstudio.com/
2.安裝vscode中的 C/C++ 擴充套件
點選左側選單的擴充功能
https://ithelp.ithome.com.tw/upload/images/20210712/20139356F88pE36bhY.png
在擴充功能的搜尋欄位搜尋 "c++"
https://ithelp.ithome.com.tw/upload/images/20210712/20139356qrGfFQWCYx.png
點選install (安裝)
使用code runner擴充功能一鍵執行
在Visual Studio code中的擴充功能搜尋"code runner"並點擊install安裝
https://ithelp.ithome.com.tw/upload/images/20210712/20139356XuR4gsXKsl.png
安裝AI Tabnine的vscode擴充功能(建議) 可以幫助你更快速的撰寫程式
一樣在擴充功能的搜尋欄位中搜尋 "tabnine"
https://ithelp.ithome.com.tw/upload/images/20210712/20139356uslkh2od9n.png
一樣點選install (安裝)
注意! 安裝完擴充套件後,記的關閉Visual Studio code來確保再次重新啟動應用程式時,這些擴充套件能正常使用
4.安裝編譯器 MinGW Compiler
http://bit.ly/CompilerFree
進入網站等待5秒後網頁就可以下載了
下載完後開啟安裝檔
點選 Next>
https://ithelp.ithome.com.tw/upload/images/20210712/20139356rY6ZraApgX.png
在 Architecture 的欄位選擇 "X86_64" 接著點選 Next>
https://ithelp.ithome.com.tw/upload/images/20210712/201393567SZ5bixRNH.png
在安裝位置的選項中把預設安裝的路徑給複製起來,或複製至記事本。
https://ithelp.ithome.com.tw/upload/images/20210712/20139356o5TeYmqGO5.png
接著點選Next>進行安裝
https://ithelp.ithome.com.tw/upload/images/20210712/20139356JPHlC8xU7I.png
安裝完後點選Next>
https://ithelp.ithome.com.tw/upload/images/20210712/20139356FfGqg3gQMH.png
再點選Finish
https://ithelp.ithome.com.tw/upload/images/20210712/20139356x1Klj0J1ME.png
設定系統Path變數
再windows搜尋框中搜尋"控制台"
https://ithelp.ithome.com.tw/upload/images/20210712/20139356JxHNakCfkM.png
在控制台中的的搜尋中搜尋關鍵字"變數"
https://ithelp.ithome.com.tw/upload/images/20210712/20139356ffzFPWPkuV.png
在系統下點選 編輯您的帳戶的環境變數
https://ithelp.ithome.com.tw/upload/images/20210712/20139356koXEn8OlN4.png
並在 使用者變數 中選擇"Path"並點選"編輯"
https://ithelp.ithome.com.tw/upload/images/20210712/20139356v9ucYMJUjM.png
接著點選"新增"
https://ithelp.ithome.com.tw/upload/images/20210712/20139356dwSAQR88Y0.png
並將剛剛我們複製的安裝路徑貼上,記的要在結尾的地方在加入 \mingw64\bin
https://ithelp.ithome.com.tw/upload/images/20210712/20139356wKQuevDM16.png
完成後選擇確定
https://ithelp.ithome.com.tw/upload/images/20210712/20139356ck1Y6iWJis.png
再選擇確定
測試是否加入成功
為了測試我們是否有成功將編譯器加入電腦,讓我們開起 命令提示字元 輸入:

g++ --version
gdb --version 

如果在這些地方卡住,就再按一次Enter鍵
https://ithelp.ithome.com.tw/upload/images/20210712/20139356Y9RzUIipsi.png
如果你剛剛有關掉Visual Studio code!就直接開啟
沒有的話請先關閉再開啟來確保擴充套件可以正常執行
https://ithelp.ithome.com.tw/upload/images/20210712/20139356PBXwXxZVLp.png
在開啟Visual Studio code後我們在任何地方建立一個空的資料夾來存放我們的C++專案
並在Visual Studio code中開啟它
點選專案管理圖示中的"Open folder"來開啟資料夾
https://ithelp.ithome.com.tw/upload/images/20210712/20139356nlF9HQkzeH.png
並選擇你剛剛建立的專案資料夾,我已test資料夾來做示範,資料夾叫什麼名子都可以
https://ithelp.ithome.com.tw/upload/images/20210712/20139356qzMT4OXgZ8.png
完成後在資料夾中心增一個 (檔名自己取).cpp 的文件來撰寫C++
https://ithelp.ithome.com.tw/upload/images/20210712/20139356lRl0bEm3Ia.png
現在可以寫一個建單的Helloworld程式並執行
cin.get(); 的用途是在.exe的執行檔中執行完畢後不要自動關閉視窗並保留視窗

#include<iostream>

using namespace std;

int main(){
cout <<"Helloworld"<<endl;

cin.get();//在.exe的執行檔中執行完畢後不要自動關閉視窗並保留視窗

return 0;
}

完成後讓我們儲存檔案Ctrl + S來執行它(使用code runner可略過此步驟,只要點擊右上方的三角形箭頭就可以執行了),點選上方的Terminal中的 Run Build Task 或快捷鍵 Ctrl + shift + B
在彈出的視窗中選擇 "C/C++:使用g++.exe建置使用中檔案"
https://ithelp.ithome.com.tw/upload/images/20210712/201393563eKgzMjaTn.png
等待在下方的終端機視窗顯示"已成功完成建置"就表示已成功建立.exe執行檔。
https://ithelp.ithome.com.tw/upload/images/20210712/20139356HtmSoDcnsJ.png
如果顯示"建置已完成,但發生錯誤."可能是執行檔還在運作中如果不是的話,建議把在和(檔名).cpp相同檔案的資料夾中的(檔名).exe執行檔給刪除,接著在重新建置一次。
https://ithelp.ithome.com.tw/upload/images/20210712/201393568qsyi1JFYO.png
接下來我們可以用以下幾種方法來執行 結束執行按下ctrl + C

1.使用code runner擴充功能一鍵執行 不須用執行上一步 "建置專案"
在Visual Studio code中的擴充功能搜尋"code runner"並點擊install安裝
https://ithelp.ithome.com.tw/upload/images/20210712/20139356XuR4gsXKsl.png
安裝完後回到專案只要點擊右上方的三角形箭頭就可以執行了
https://ithelp.ithome.com.tw/upload/images/20210712/20139356hbhehYzfb6.png

2.使用終端機執行
在vscode中開啟終端機從下往上拉 或點選上方的 Terminal > New Terminal 或開啟其他終端機應用程式,
在終端機中進入專案資料夾並輸入 :
./(檔名).exe
在下次執行時可使用方向建來再次輸入指令,或用指令cls來清空畫面

3.使用.exe執行檔執行
在資料夾中開啟(檔名).exe來執行,注意! .cpp檔案中必須含有迴圈或在 return 0; 前加入 cin.get();,不然一執行完就會馬上自動關閉執行視窗。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言