Compiling openCV 2.3.1 programs with MinGW gcc/g++ on Windows 7 64bit-Collection of common programming errors

  • In case someone else needs to solve this issue, here’s how I got the posted OpenCV/HighGUI sample code to compile in Windows 7 x64 using MinGW, MSYS, and CMake:

    1. build OpenCV from source using MinGW/MSYS/CMake. This is because I could not get the MinGW compiled version in the OpenCV-win-SuperPack to link properly in MinGW/MSYS/Windows 7 x64.

      For full reference, here’s how I compiled OpenCV:

      • make sure you have an up-to-date CMake (v2.6 or later) and MinGW (with GCC, G++, and MSYS options) installed
      • if you want the new Qt-based OpenCV HighGUI front-end, you will need to install Qt 4 (SDK).
      • download a OpenCV source/superpack version 2.2 or later (I used OpenCV-2.3.1-win-superpack.exe)
      • unzip the contents to [OPENCV_SOURCE_DIR] (I put it in C:/opencv, so there should be a file at C:/opencv/README for example)
      • create a [OPENCV_BUILD_DIR] directory elsewhere (I used C:/opencv/build/mingw)
      • use the CMake-GUI tool, specify the source directory as [OPENCV_SOURCE_DIR], the build directory as [OPENCV_BUILD_DIR], and click “Configure”.
      • you may wish/need to go tweak the options (e.g. I ticked “Qt” and “Qt-OpenGL” entries, then clicked “Configure” again, then had to provide the path to the qmake executable)
      • once you have finished configuring OpenCV, click “Generate”
      • in a MSYS terminal, browse to [OPENCV_BUILD_DIR], and run “make” to build the code (this may take a while)
      • once the has been built properly, run “make install”, which collects the built code/libraries/include dirs into [OPENCV_BUILD_DIR]/install folder (or a different folder if you changed the corresponding option when using the CMake-GUI tool)
      • add [OPENCV_BUILD_DIR]/install/bin folder to the PATH environmental variable. If you do not know how to do this, then I’d recommend using the Path Editor GUI tool.
      • if you end up using Qt, you will also need to put the bin folder of Qt SDK in the PATH environmental variable. This is the folder that includes qmake.exe.
    2. put the following sample code into a file called test.c. I modified the includes slightly to make them compatible with OpenCV v2.2 and above.

       int main(int argc, char *argv[])
         // Nothing but create a window
         cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
         cvMoveWindow("mainWin", 100, 100);
         return 0;
    3. in a MSYS terminal, browse to the folder where you put test.c, and run:

      gcc -o test -I"[OPENCV_BUILD_DIR]/install/include" test.c \
        -L"[OPENCV_BUILD_DIR]/install/lib" \
        -lopencv_core[OPENCV_VERSION] \
        -lopencv_imgproc[OPENCV_VERSION] \

      So in my case:

      gcc -o test -I"/c/opencv/build/mingw/install/include" test.c \
        -L"/c/opencv/build/mingw/install/lib" \

    Path Editor:

  • You have the directory, C:\opencv\install\bin, to locate libraries on the gcc/g++ command line, but I think you’ll also need to specify the libraries to use as linker inputs as well. I’m not sure what libraries are part of the OpenCV distribution, but going by the example on the instruction page you linked to, one might be:


    You’ll probably have to add one or more other ones (that follow the name pattern lib*.a in the C:\opencv\install\bin directory – or maybe some other lib directory that you should be passing in a -L option).

Originally posted 2013-11-06 03:16:27.