Visual Studio Code OpenCV(C++)開發環境設定


Contents


Step 1: 安裝MinGW

下載MinGW-w64

MinGW-w64

 

開始安裝

 

設定環境變數

 

測試

打開cmd,執行指令:g++ --version

 

Step 2: 編譯OpenCV

建立資料夾

 

開始編譯

勾選: 

BUILD_opencv_world 

WITH_OPENGL  

取消勾選: 

OPENCV_ENABLE_ALLOCATOR_STATS

打開cmd,執行指令:mingw32-make -j4

執行指令:mingw32-make install

 

設定環境變數

 

Step 3: VSCode環境設定

安裝C/C++ Extension

 

新增c_cpp_properties.json

 

按下Ctrl+Shift+P後,選擇Edit Configurations

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\opencv\\build\\x64\\MinGW\\install\\include",
                "C:\\opencv\\build\\x64\\MinGW\\install\\include\\opencv",
                "C:\\opencv\\build\\x64\\MinGW\\install\\include\\opencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

 

新增launch.json

 

按下Ctrl+Shift+D後,選擇create a launch.json file

 

選擇C++(GDB/LLDB)

 

選擇g++.exe

 

忽略Error,編輯launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 建置及偵錯使用中的檔案",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "啟用 gdb 的美化顯示",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 建置使用中檔案"
        }
    ]
}

 

新增tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 建置使用中檔案",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-I", "C:\\opencv\\build\\x64\\MinGW\\install\\include",
                "-I", "C:\\opencv\\build\\x64\\MinGW\\install\\include\\opencv",
                "-I", "C:\\opencv\\build\\x64\\MinGW\\install\\include\\opencv2",
                "-L", "C:\\opencv\\build\\x64\\MinGW\\install\\x64\\mingw\\lib",
                "-l", "libopencv_world3412"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "偵錯工具產生的工作。"
        }
    ],
    "version": "2.0.0"
}

 

測試

#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main()
{
    Mat img = imread("flower.jpg");
    resize(img, img, Size(0, 0), 0.1, 0.1);
    imshow("test", img);
    waitKey();
    destroyAllWindows();
    system("PAUSE");
    return 0;
}

按下F5,開始Debug 

完成!!!

留言