Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not sure how to build OpenCV for MinGW

Tags:

c++

opencv

cmake

TL;DR;

I am trying to build OpenCV for MinGW. cmake keeps complaining about no sane CXX compiler, although I can compile sample code with g++.exe.


Long version

I tried finding a binary for MinGW, but this didn't work.

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.

E:\work\opencv\mybuild> cmake -G "MinGW Makefiles" ../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 "MinGW Ma
kefiles".  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 "MinGW Ma
kefiles".  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!

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.

E:\work\opencv\mybuild> cmake -G "MSYS Makefiles" ../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 "C:/MinGW/bin/g++.exe" is not able to compile a simple
  test program.

  It fails with the following output:

   Change Dir: E:/work/opencv/mybuild/CMakeFiles/CMakeTmp

The error log is:

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 "cmTryCompileExec1507552824/fast"
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'

"/C/Program Files (x86)/CMake 2.8/bin/cmake.exe" -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

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.


EDIT:

I tried barek's answer, and the error is the same as with MSYS target - no sane c++ compiler. I next tried cmake-gui, specifying compilers. Also, I made sure that MinGW/bin is in my path. Follows the error message:

CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  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 "MinGW Makefiles".  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!

I suspect the problem lies in somethign really stupid, like CMAKE not being able to change directories, due to impropper MinGW isntallation.

Here a discussion can be found, but I:

  • Checked that MinGW/bin is in my path
  • Also specified the C and C++ compiler paths manually in cmake-gui
  • Installed MinGW with the installer from SourceForge and selected the tickbox "Alter PATH".
like image 213
Vorac Avatar asked Dec 26 '22 08:12

Vorac


1 Answers

  1. cleanup! delete all previous build attempts, CmakeFile.cache, etc.

  2. have MingW/bin on the path

  3. make a folder within the opencv dir, called build_mingw, cd there

  4. cmake -G "MinGW Makefiles" -D CMAKE_CXX_COMPILER=mingw32-g++.exe -D CMAKE_MAKE_PROGRAM=mingw32-make.exe .. # <-- note the dots at the end

  5. mingw32-make

  6. mingw32-make install

like image 133
berak Avatar answered Dec 28 '22 21:12

berak