Setting Up OpenCV 4.5.0 with MinGW-w64, CMake, and VSCode on Windows 10

1. Prepare the C++ Development Environment with VSCode and MinGW-w64

Download MinGW-w64 from SourceForge.

Add the bin folder (e.g., C:\Programs\mingw-w64\bin) to the system PATH.

2. Install CMake

Get CMake from the official site.

Add the bin directory (e.g., D:\tools\cmake-3.19.1-win64-x64\bin) to PATH.

3. Download OpenCV Sources

Fetch the source code from the OpenCV releases page. For faster downloads, use a local mirror.

Extract the archive to a target directory, e.g., D:\tools\opencv. Inside build\x64, create a new folder named mingw.

4. Generate Makefiles with CMake GUI

Launch cmake-gui.exe from the CMake bin folder.

  • Set the source code path to the extracted OpenCV folder.
  • Set the build path to D:\tools\opencv\build\x64\mingw.
  • Click Configure. When prompted, select MinGW Makefiles and specify the MinGW compilers.
  • Click Finish to begin configuration. Wait until Configure done appears.
  • Click Generate to create the Makefiles. A Generate Done message confirms success.

5. Compile and Install OpenCV

Open a command prompt and change to the build directory:

cd D:\tools\opencv\build\x64\mingw

Run the build with multiple threads (adjust the number to match your CPU):

mingw32-make -j 4

Troubleshooting: If an error like vs_version.rc.obj missing appears, locate the corresponding line in build.make (e.g., line 1494). Manually compile the missing object file in the modules/core/CMakeFiles/opencv_core.dir subdirectory using the command from that line, then rerun mingw32-make.

After succesful compilation, install the libraries:

mingw32-make install

This creates an install folder under mingw. Add the install\bin directory to the system PATH (e.g., D:\tools\opencv\build\x64\mingw\install\bin).

6. Configure VSCode

Inside your project .vscode folder, update the following files.

c_cpp_properties.json

Add include paths:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}",
                "D:/tools/opencv/build/x64/mingw/install/include",
                "D:/tools/opencv/build/x64/mingw/install/include/opencv2"
            ]
        }
    ]
}

tasks.json

Define build arguments. The following example compiles the current file and links OpenCV libraries:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${workspaceRoot}/zzz_output_exe_files/${fileBasenameNoExtension}.exe",
                "-I", "D:/tools/opencv/build/x64/mingw/install/include",
                "-I", "D:/tools/opencv/build/x64/mingw/install/include/opencv2",
                "-L", "D:/tools/opencv/build/x64/mingw/lib",
                "-l", "libopencv_calib3d450",
                "-l", "libopencv_core450",
                "-l", "libopencv_dnn450",
                "-l", "libopencv_features2d450",
                "-l", "libopencv_flann450",
                "-l", "libopencv_gapi450",
                "-l", "libopencv_highgui450",
                "-l", "libopencv_imgcodecs450",
                "-l", "libopencv_imgproc450",
                "-l", "libopencv_ml450",
                "-l", "libopencv_objdetect450",
                "-l", "libopencv_photo450",
                "-l", "libopencv_stitching450",
                "-l", "libopencv_video450",
                "-l", "libopencv_videoio450"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Note: The library names (e.g., libopencv_core450) must match the actual .a files in the lib folder. The last three digits reflect the OpenCV version (4.5.0 → 450). Adding libopencv_videoio450 is essential for video handling; without it, VideoCapture will fail.

7. Test the Setup

Create a test file test.cpp in the project root:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main(int argc, char** argv) {
    Mat img = imread("images/man.jpg");
    if (img.empty()) {
        std::cerr << "Could not read the image." << std::endl;
        return -1;
    }
    imshow("Display Window", img);
    waitKey(0);
    return 0;
}

Place a sample image at images/man.jpg in the same directory as .vscode. Build and run; a window should display the image.

Tags: OpenCV MinGW-w64 CMake VSCode Windows 10

Posted on Mon, 18 May 2026 10:02:31 +0000 by quercus