# 建置VScode的C++及OpenCV環境 by076 ## Step 1: 下載VScode Windows x64 Stable :::info https://code.visualstudio.com/ ::: ## Step 2: 下載Mingw64 :::info https://nchc.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z ::: ## Step 3: 下載OpenCV :::info https://drive.google.com/file/d/1UXtVSnCI7BTCAYuYgf2GQph7flR-UqGj/view?usp=sharing ::: ## Step4: 建置環境變數 ```mermaid graph LR 編輯系統環境變數:::someclass --> 環境變數 classDef someclass fill; 環境變數:::someclass --> 系統變數 classDef someclass fill; 系統變數:::someclass --> Path classDef someclass fill; Path:::someclass --> 新增Mingw的bin路徑 classDef someclass fill; 新增Mingw的bin路徑:::someclass --> 新增OpenCV的bin路徑 classDef someclass fill; ``` :::info 編輯系統環境變數➜ 環境變數 ➜ 系統變數 ➜ Path ➜ 新增 Mingw的bin路徑 (ex: D:\APP\Mingw\mingw64\bin) ➜ 新增OpenCV的bin路徑 (ex: D:\APP\Opencv\build\bin) ::: ## Step5: 把OpenCV裡的include和lib分別複製到mingw64中的include和lib :::info - include >D:\APP\Opencv\build\include內的"opencv2"文件複製到 D:\APP\Mingw\mingw64\include - lib >D:\APP\Opencv\build\lib 內的 "全部資料"複製到 D:\APP\Mingw\mingw64\lib ::: ## Step6: 建置一個資料夾並建立test.cpp,在資料夾中右鍵"以Code開啟"  ## Step7: 點開test.cpp,貼上範例程式 - **include出現紅線先呼略啦** - **test.png記得準備啦** ```cpp= #include "opencv2/opencv.hpp" #include "iostream" int main(int argc, char const *argv[]) { cv::Mat img = cv::imread("test.png"); if (img.empty()) std::cout << "image is empty or the path is invalid!" << std::endl; cv::imshow("Origin", img); cv::waitKey(0); cv::destroyAllWindows(); return 0; } ``` ### :pushpin: Hint: 終端機的開法  ## Step8: 測試時間 - ### 打在終端機上 :::info g++ -g test.cpp -I <font color=red>D:\\\\APP\\\\Mingw\\\\mingw64\\\\include</font> -lopencv_core452 -lopencv_imgcodecs452 -lopencv_imgproc452 -lopencv_calib3d452 -lopencv_dnn452 -lopencv_features2d452 -lopencv_flann452 -lopencv_gapi452 -lopencv_highgui452 -lopencv_ml452 -lopencv_objdetect452 -lopencv_photo452 -lopencv_stitching452 -lopencv_video452 -lopencv_videoio452 -o test.exe <font color=red> 紅色的地方改成自己mingw64 include路徑 </font> ::: ## Step9: 執行資料夾內生成的test.exe - ### 成功顯示test.png ➜ 測試成功 ## Step10: 在Coding用資料夾內建立名為.vscode的資料夾,在裡面新增tasks.json ## Step11: 撰寫tasks.json - ### 從範例修改 :::info https://drive.google.com/file/d/11YXBBIR8iFmvafgwSsAu7J7pzxvkYABR/view?usp=sharing :::   ## Step12: 存檔後到test.cpp執行結果,有以下兩種方式: :::info >### **1. Shift+ctrl+B : 只編譯出exe檔,要再手動執行exe。** > >### **2.**  >### **案下去就會編譯加執行exe啦!!** ::: ### **:pushpin: Hint: 若出現此現象,則選擇g++編譯器**  ## <補充>: 出現紅線 (ex: include出現紅線) 的解決辦法 ### 1. **使用shift+ctrl+P叫出搜尋欄,輸入C++ config**  ### 2. **修改內文** 
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.