MinGW is a collection of freely available and freely distributable Windows specific header files and import libraries, augmenting the GNU Compiler Collection, (GCC), and its associated tools, (GNU binutils). MinGW provides a complete Open Source programming tool set which is suitable for the development of native Windows programs that do not depend on any 3rd-party C runtime DLLs.
Compile OpenCV under MinGW.
MinGW can be used on its own but it is also the "favorite" compiler of the following IDE's:
Eclipse/CDT (Windows)
Code::Blocks (Windows, Linux & Mac)
DevCpp (Windows)
Compiling OpenCV with MinGW is shown here differently for different versions of OpenCV:
OpenCV 2.2
In order to make OpenCV 2.2 (containing C++ code) work under Windows with Code::Blocks and MinGW, Matthew (mazeus12 on the mailing list) did the following:
As mentioned in "OpenCV-2.2.0-win-README.txt", "OpenCV-2.2.0-win32-vs2010.exe" does not contain binaries for MinGW so they need to be built from the contents in "OpenCV-2.2.0-win.zip".
Steps to build OpenCV 2.2 with Code::Blocks and MinGW:
1. Install Code::Blocks (10.05) with the (MinGW) C++ compiler option. This should among other install the C++ compiler and mingw32-make to "C:\Program Files\CodeBlocks\MinGW\bin" (I also tried to install the latest MinGW using "mingw-get install gcc g++ mingw32-make" from www.mingw.org but I got an error in extracting some files…)
2. Add "C:\Program Files\CodeBlocks\MinGW\bin" to system PATH (at your own judgment: remove any other paths to MinGW (Somehow DevCpp MinGW paths with probable different versions messed up the build process))
3. Install Cmake (2.8)
4. Extract "OpenCV-2.2.0-win.zip" to "C:\OpenCV-2.2.0-win" (It creates a second folder so the final destination looks like that: "C:\OpenCV-2.2.0-win\OpenCV-2.2.0")
5. Run Cmake (cmake-gui)
6. Set the source code: "C:\OpenCV-2.2.0-win\OpenCV-2.2.0"
7. Set where to build the binaries: e.g. "C:\OpenCV2.2MinGW"
8. Press configure
9. Let Cmake create the new folder
10. Specify the generator: MinGW Makefiles
11. Select "Specify native compilers" and click next
12. For C set: C:/Program Files/CodeBlocks/MinGW/bin/gcc.exe
13. For C++ set: C:/Program Files/CodeBlocks/MinGW/bin/g++.exe
14. Click finish
15. In the configuration screen type in "RELEASE" (or "DEBUG" if you want to build a debug version) for "CMAKE_BUILD_TYPE". Select BUILD_EXAMPLES if you want (I didn't change anything else here like "WITH_TBB" or "WITH QT" etc. I'll try that when time comes to use TBB or Qt)
16. Click configure again
17. Click generate
18. Close cmake
19. Go to the command prompt and inside the folder "C:\OpenCV2.2MinGW" type "mingw32-make" and hit enter (takes some time)
20. Then type "mingw32-make install" and hit enter again
21. Open Code::Blocks and create a new C++ project (Configuration similar to the guide: CodeBlocks).
22. In menu: "Project/Build options/Linker settings/Link libraries" add "C:\OpenCV2.2MinGW\lib\libopencv_calib3d220.dll.a" and all the other *.dll.a files in this folder
23. In menu: "Project/Build options/Search directories/Compiler" add "C:\OpenCV2.2MinGW\include" (includes in a new program schould look like this: "#include <opencv2/core/core.hpp>, #include <opencv2/imgproc/imgproc.hpp>, #include <opencv2/highgui/highgui.hpp> etc.")
24. In menu: "Project/Build options/Search directories/Linker" add "C:\OpenCV2.2MinGW\lib"
The otions in steps 22 - 24 can also be added as global options in menu: Settings/Compiler an Debugger/Global compiler settings/..., so they will apply to any project and opened *.cpp file.
25. If necessary, in menu: Settings/Compiler an Debugger/Global compiler settings/Toolchain executables" specify "C:\Program Files\CodeBlocks\MinGW" for the compiler's installation directory.
26. Open a sample file (or import it into the project), e.g. "C:\OpenCV2.2MinGW\samples\cpp\dft.cpp" and built it. (If the *.cpp files do not exist in this folder, you can find them in the initial folder where you extracted "OpenCV-2.2.0-win.zip" i.e. "C:\OpenCV-2.2.0-win\OpenCV-2.2.0\samples\cpp")
27. Add "C:\OpenCV2.2MinGW\bin" to the system path
28. Run the program
OpenCV 2.0 and 2.1
Release package
OpenCV 2.0 and 2.1 are distributed as a package fromsourceforge
Note: This package is only in Release mode (if you want to have the Debug configuration you need to compile it)
Compilation
With its new c++ interface OpenCV needs GCC 4.x (Since version 1.1.0).
You need to download and install MinGW:
Therefore you need to use the official release of MinGW with GCC 4.4.x (released in June 22, 2009 see MinGW GCC 4.4 release note).
Also, there is an unofficial release which use GCC 4.x called MinGW TDM
Once the installation is done, you can look at the InstallGuide to continue the compilation.
After compilation you need to be sure that you link with the generatedlibcv*.a(static version of shared library), and that yourlibcv*.dllare in thePATH.
Note: You can have in the same folder the *.dll for MinGW, MSVC and OpenCV 1.0.0. However you need to be sure to not mix them during the linking.
Tips
TBB library with MinGW
Since version 2.1, OpenCV use the TBB library to support parallel processing:
To build OpenCV with TBB under MinGW. You should compiled TBB sources with the following command:
make compiler=gcc arch=ia32 runtime=mingw tbb
Then you should set :
- TBB_INCLUDE_DIR to "include" subfolder in your tbb source folder
- TBB_LIB_DIR to "build\windows_ia32_gcc_mingw_release" subfolder.
Known Bug specific to MinGW
Building on Windows using MinGW 3.4.5
(Perhaps this is not relevant for OpenCV 2.1)
If you get compile errors to do with __exchange_and_add it is due to the wrong definition of CV_XADD. It can be fixed by:
Open cxoperations.hpp (found in <Open CV base dir>\include\opencv)
- Find this section (lines 67-68 in 2.0):
#else #include <bits/atomicity.h> #if __GNUC__ >= 4 And update it to reference a new definition, __MINGW32__ (defined by the MinGW compiler, see http://predef.sourceforge.net/precomp.html#sec31):
#else #include <bits/atomicity.h> #if __GNUC__ >= 4 || __MINGW32__
OpenCV 1.0
First, you need to download the official release of MinGW at sourceforge.net
You can use directly the win32 binary package (*.dll and *.lib) because C code compile under Microsoft Visual Studio is compatible with MinGW.
However, the VideoSurveillance part can not be used as it include c++ code.