title: Not sure how to build OpenCV for MinGW
body: <p>TL;DR;</p>
  <p>I am trying to build OpenCV for MinGW. <code>cmake</code> keeps complaining about no sane CXX compiler, although I can compile sample code with g++.exe.</p>
  <hr />
  <p>Long version</p>
  <p>I tried finding a binary for MinGW, <a href="https://stackoverflow.com/questions/24094538/problems-linking-to-opencv-from-mingw">but this didn't work</a>.</p>
  <p>I tried compiling for MinGW but that fails with some error about sh.exe not being in $PATH. I am puzzled, as renaming the target file on the filesystem did not resolve the issue.</p>
  <pre><code>E:\work\opencv\mybuild&gt; cmake -G &quot;MinGW Makefiles&quot; ../sources
  CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeMin
  GWFindMake.cmake:22 (message):
    sh.exe was found in your PATH, here:
  
  
    C:/Users/desuna/AppData/Local/GitHub/PortableGit_054f2e797ebafd44a30203088cd3d
  58663c627ef/bin/sh.exe
  
  
    For MinGW make to work correctly sh.exe must NOT be in your path.
  
    Run cmake from a shell that does not have sh.exe in your PATH.
  
    If you want to use a UNIX shell, then use MSYS Makefiles.
  
  Call Stack (most recent call first):
    CMakeLists.txt:56 (project)
  
  
  CMake Error: CMake was unable to find a build program corresponding to &quot;MinGW Ma
  kefiles&quot;.  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a differe
  nt build tool.
  CMake Error: CMake was unable to find a build program corresponding to &quot;MinGW Ma
  kefiles&quot;.  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a differe
  nt build tool.
  CMake Error: Error required internal CMake variable not set, cmake may be not be
   built correctly.
  Missing variable is:
  CMAKE_CXX_COMPILER_ENV_VAR
  CMake Error: Error required internal CMake variable not set, cmake may be not be
   built correctly.
  Missing variable is:
  CMAKE_CXX_COMPILER
  CMake Error: Could not find cmake module file: E:/work/opencv/mybuild/CMakeFiles
  /2.8.12.2/CMakeCXXCompiler.cmake
  CMake Error: Error required internal CMake variable not set, cmake may be not be
   built correctly.
  Missing variable is:
  CMAKE_C_COMPILER_ENV_VAR
  CMake Error: Error required internal CMake variable not set, cmake may be not be
   built correctly.
  Missing variable is:
  CMAKE_C_COMPILER
  CMake Error: Could not find cmake module file: E:/work/opencv/mybuild/CMakeFiles
  /2.8.12.2/CMakeCCompiler.cmake
  CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
  CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
  -- Configuring incomplete, errors occurred!
  </code></pre>
  <p>I was advised later, that MSYS makefiles do not have this requirement. Now there was an error about the C++ compiler. However, the C++ compiler is correctly identified. Also, I tried compiling a dummy test.cxx program and it worked.</p>
  <pre><code>E:\work\opencv\mybuild&gt; cmake -G &quot;MSYS Makefiles&quot; ../sources
  -- The CXX compiler identification is GNU 4.8.1
  -- The C compiler identification is GNU 4.8.1
  -- Check for working CXX compiler: C:/MinGW/bin/g++.exe
  -- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- broken
  CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTes
  tCXXCompiler.cmake:54 (message):
    The C++ compiler &quot;C:/MinGW/bin/g++.exe&quot; is not able to compile a simple
    test program.
  
    It fails with the following output:
  
     Change Dir: E:/work/opencv/mybuild/CMakeFiles/CMakeTmp
  </code></pre>
  <p>The error log is:</p>
  <pre><code>Determining if the CXX compiler works failed with the following output:
  Change Dir: E:/work/opencv/mybuild/CMakeFiles/CMakeTmp
  
  Run Build Command:C:/PROGRA~2/GnuWin32/bin/make.exe &quot;cmTryCompileExec1507552824/fast&quot;
  C:/PROGRA~2/GnuWin32/bin/make.exe -f CMakeFiles/cmTryCompileExec1507552824.dir/build.make CMakeFiles/cmTryCompileExec1507552824.dir/build
  
  make.exe[1]: Entering directory `E:/work/opencv/mybuild/CMakeFiles/CMakeTmp'
  
  &quot;/C/Program Files (x86)/CMake 2.8/bin/cmake.exe&quot; -E cmake_progress_report /E/work/opencv/mybuild/CMakeFiles/CMakeTmp/CMakeFiles 1
  
  Building CXX object CMakeFiles/cmTryCompileExec1507552824.dir/testCXXCompiler.cxx.obj
  /C/MinGW/bin/g++.exe     -o CMakeFiles/cmTryCompileExec1507552824.dir/testCXXCompiler.cxx.obj -c /E/work/opencv/mybuild/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
  
  process_begin: CreateProcess(NULL, /C/MinGW/bin/g++.exe -o CMakeFiles/cmTryCompileExec1507552824.dir/testCXXCompiler.cxx.obj -c /E/work/opencv/mybuild/CMakeFiles/CMakeTmp/testCXXCompiler.cxx, ...) failed.
  
  make (e=2): The system cannot find the file specified.
  
  
  make.exe[1]: *** [CMakeFiles/cmTryCompileExec1507552824.dir/testCXXCompiler.cxx.obj] Error 2
  
  make.exe[1]: Leaving directory `E:/work/opencv/mybuild/CMakeFiles/CMakeTmp'
  
  make.exe: *** [cmTryCompileExec1507552824/fast] Error 2
  </code></pre>
  <p>My aim is to have one-step build under Windows 7 for a small application in C++, that links against onecv_core, opencv_highgui and several more. For this, it appears that I need to build OpenCV from source with gcc under mingw. Please advise what is the correct approach to that.</p>
  <hr />
  <p>EDIT:</p>
  <p>I tried <strong>barek</strong>'s answer, and the error is the same as with MSYS target - no sane c++ compiler. I next tried <code>cmake-gui</code>, specifying compilers. Also, I made sure that MinGW/bin is in my path. Follows the error message:</p>
  <pre><code>CMake Error: CMake was unable to find a build program corresponding to &quot;MinGW Makefiles&quot;.  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
  CMake Error: CMake was unable to find a build program corresponding to &quot;MinGW Makefiles&quot;.  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
  CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
  Missing variable is:
  CMAKE_CXX_COMPILER_ENV_VAR
  CMake Error: Could not find cmake module file: E:/work/opencv/mybuild/CMakeFiles/2.8.12.2/CMakeCXXCompiler.cmake
  CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
  Missing variable is:
  CMAKE_C_COMPILER_ENV_VAR
  CMake Error: Could not find cmake module file: E:/work/opencv/mybuild/CMakeFiles/2.8.12.2/CMakeCCompiler.cmake
  Configuring incomplete, errors occurred!
  </code></pre>
  <p>I suspect the problem lies in somethign really stupid, like CMAKE not being able to change directories, due to impropper MinGW isntallation.</p>
  <p><a href="http://ogre3d.org/forums/viewtopic.php?f=2&amp;t=55238" rel="nofollow noreferrer">Here</a> a discussion can be found, but I:</p>
  <ul>
  <li>Checked that MinGW/bin is in my path</li>
  <li>Also specified the C and C++ compiler paths manually in <code>cmake-gui</code></li>
  <li>Installed MinGW with the installer from SourceForge and selected the tickbox &quot;Alter PATH&quot;.</li>
  </ul>