Build OpenCV from source in Windows 10 (MinGW, CMake)

This article describes the steps for getting everything configured and running to build OpenCV from source on a Windows 10 machine (as of the 14th of August, 2016). One can install OpenCV using apt-get in the bash shell of Windows which was recently introduced, but this article is not about that. This article is about natively building OpenCV using MinGW on Windows. If one wants, one can also use a different compiler like Visual C++, but I have not tried it. OpenCV supports Visual C++ pre-built binaries, and so if one wants to develop using Visual Studio, one can directly get those binaries instead of building from source.

The main purpose of this article was to create a Linux like environment to work with OpenCV in Windows, and to not depend on Visual Studio for development.


Step 1: Install MinGW

Install MinGW to the default location of C:\MinGW with gcc, g++ and make.exe (MSYS). Then add C:\MinGW\bin to the System path by adding it to the PATH environment variable.

Step 2: Download OpenCV

Get OpenCV 2.4 from Itseez’s Github page by cloning the repository to where you want OpenCV to be located (like C:\opencv). Clone by running this command in the command prompt.

git clone

(You can also download the .zip file from GitHub but it is nice to have Git installed.)

Step 3: Configure CMake

Open the CMake GUI and select C:\opencv as the source directory (CMakeLists.txt should be present in C:\opencv). Create a directory in the opencv folder and name it build. And create a directory inside of build and name it mingw incase you want to build using a different compiler later on. Now, click on Configure, choose MinGW makefiles or MSYS makefiles depending on what make.exe you installed. I use MSYS make.exe, so MSYS makefiles. I also manually provide the gcc, g++ paths.

If you have trouble configuring, just know that you need to specify the C++ compilers which is in C:\MinGW\bin.

After all the configuration is done, click Generate.

Step 4: Build the source code

Open command prompt, navigate to C:\opencv. You should see a Makefile. Run make.exe. If you did MinGW makefiles, then run C:\MinGW\bin\make.exe, otherwise if you did MSYS makefiles, then run C:\MinGW\msys\1.0\bin\make.exe.

I had an error with HMODULE in the module opencv_videoio which contains highgui. I made a temporary fix by commenting out cv_GetCurrentModule function altogether and setting HMODULE m = 0; at line 129 in the cap_ffmpeg.cpp file. Instead of doing all that, I believe #define _WIN32_WINNT 0x0501 at the top would work too.

Step 5: Writing a demo program

Create a cam_test folder somewhere to write OpenCV code. Write the following code and build it.

// cam_test.cpp

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highghui.hpp>

int main(int argc, char *argv[])
    if (argc < 2)
        return 1;

    cv::Mat image;

    image = cv::imread(argv[1], cv::IMREAD_COLOR);

    if (!
        std::cout << "Could not open image." << std::endl;
        return 1;

    cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE);
    cv::imshow("Display Image", image);


    return 0;
# CMakeLists.txt

cmake_minimum_Version_required(VERSION 2.8)

# Set the directory to wherever you built opencv
set(OpenCV_DIR "C:/opencv/build/mingw/")

find_package(OpenCV REQUIRED)

add_executable(${PROJECT_NAME} cam_test.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

Run the following commands (might be different depending on your MinGW configuration) –

> cmake -D"CMAKE_MAKE_PROGRAM:PATH=C:/MinGW/msys/1.0/bin/make.exe" -G "MSYS Makefiles"
> C:\MinGW\msys\1.0\bin\make.exe

Or, open up the project in Qt Creator because it has native support for CMake projects, change the compiler in Tools -> Options -> Build & Run -> Compilers to point to MinGW’s compilers, and run it there.

Leave a Reply

Your email address will not be published. Required fields are marked *