编译 OpenCV
打开终端进行编译:(-j 是使用 8 个线程进行编译,请根据你的计算机配置合理设置线程数)
E:
cd E:\opencv_341\opencv_mingw64_build
mingw32-make -j 8
mingw32-make install
如果 mingw32-make -j 8
遇到错误,请看下面的 编译 OpenCV 常见错误,否则执行 mingw32-make install
,完成安装。
编译 OpenCV 常见错误
- MinGW-w64 的 aviriff.h 文件注释错误
表现:
[ 49%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj
In file included from E:\opencv_341\opencv\sources\modules\videoio\src\cap_dshow.cpp:113:0:
e:\mingw-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\x86_64-w64-mingw32\include\aviriff.h:2:8: error: expected constructor, destructor, or type conversion before 'file'
- This file is part of the mingw-w64 runtime package.
^
e:\mingw-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\x86_64-w64-mingw32\include\aviriff.h:3:25: error: 'refer' does not name a type
- No warranty is given; refer to the file DISCLAIMER within this package.
^
In file included from e:\mingw-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\x86_64-w64-mingw32\include\aviriff.h:19:0,
from E:\opencv_341\opencv\sources\modules\videoio\src\cap_dshow.cpp:113:
e:\mingw-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\x86_64-w64-mingw32\include\pshpack2.h:7:21: error: expected declaration before end of line
#pragma pack(push,2)
^
modules\videoio\CMakeFiles\opencv_videoio.dir\build.make:146: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj' failed
mingw32-make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_dshow.cpp.obj] Error 1
CMakeFiles\Makefile2:3057: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
mingw32-make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
解决
打开E:\MinGW-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\x86_64-w64-mingw32\include\aviriff.h
发现第一行的多行注释少了个/符号,加上保存,如下:
/**
* This file is part of the mingw-w64 runtime package.
* No warranty is given; refer to the file DISCLAIMER within this package.
*/
然后重新 Configure-Generate-mingw32-make
就好了。
- cap_msmf.cpp capture code 错误
解决
在 cmake-gui 编译配置中:
不勾选 WITH_MSMF
然后重新 Configure-Generate-mingw32-make
- ‘M_PI’ was not declared in this scope 错误
表现:
E:\opencv-4.0.0-alpha\opencv-4.0.0-alpha\modules\calib3d\src\chessboard.cpp:2879:29: error: 'M_PI' was not declared in this scope
if(angle_temp > M_PI*0.5)
^
modules\calib3d\CMakeFiles\opencv_calib3d.dir\build.make:137: recipe for target 'modules/calib3d/CMakeFiles/opencv_calib3d.dir/src/chessboard.cpp.obj' failed
mingw32-make[2]: *** [modules/calib3d/CMakeFiles/opencv_calib3d.dir/src/chessboard.cpp.obj] Error 1
CMakeFiles\Makefile2:3018: recipe for target 'modules/calib3d/CMakeFiles/opencv_calib3d.dir/all' failed
mingw32-make[1]: *** [modules/calib3d/CMakeFiles/opencv_calib3d.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
解决
在最新的 master 分支已经解决了这个问题,见我的 PR :M_PI changed to CV_PI
如果你是在 官网 或者 github.com/opencv/opencv/releases 中下的 OpenCV 4.0.0-alpha,可能还会有这个问题,那么你需要把 chessboard.cpp、chessboard.hpp、test_chesscorners.cpp 文件中的 M_PI 全部改为 CV_PI,如我的 commit 所示:M_PI changed to CV_PI (#12645)
然后重新 Configure-Generate-mingw32-make
- ‘posix_memalign’ was not declared in this scope 错误
表现:
[ 28%] Building CXX object modules/CMakeFiles/ade.dir/__/3rdparty/ade/ade-0.1.1c/sources/ade/source/alloc.cpp.obj
E:\opencv-4.0.0-rc\opencv-4.0.0-rc-build\3rdparty\ade\ade-0.1.1c\sources\ade\source\alloc.cpp: In function 'void* ade::aligned_alloc(std::size_t, std::size_t)':
E:\opencv-4.0.0-rc\opencv-4.0.0-rc-build\3rdparty\ade\ade-0.1.1c\sources\ade\source\alloc.cpp:31:16: error: 'posix_memalign' was not declared in this scope
auto res = posix_memalign(&ret, std::max(sizeof(void*), alignment), size);
^~~~~~~~~~~~~~
mingw32-make[2]: *** [modules\CMakeFiles\ade.dir\build.make:63: modules/CMakeFiles/ade.dir/__/3rdparty/ade/ade-0.1.1c/sources/ade/source/alloc.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:884: modules/CMakeFiles/ade.dir/all] Error 2
mingw32-make: *** [Makefile:162: all] Error 2
解决
把 opencv-4.0.0-rc-build\3rdparty\ade\ade-0.1.1c\sources\ade\source\alloc.cpp 文件的所有 WIN32 改为 _WIN32,如这个 PR 所做的修改:fix check for win32 #6
然后重新 Configure-Generate-mingw32-make
来自:
https://blog.huihut.com/2018/07/31/CompiledOpenCVWithMinGW64/