OpenCV使用CMake和MinGW-w64的编译安装

前言

之前写过的一篇博文:OpenCV使用CMake和MinGW的编译安装及其在Qt配置运行 是使用 32 位的 MinGW 在 Windows 下编译 OpenCV 生成 32 位的 dll。

而这篇博文是使用 64 位的 MinGW 编译 OpenCV 生成 64 位的 dll。

因为博主没有 64 位 qmake,所以没勾选 WITH_QT

编译好的 OpenCV(MinGW 版):

Github . huihut/OpenCV-MinGW-Build

软件环境

安装 MinGW-w64 并配置环境变量

下载安装:MinGW-x64-4.8.1-release-posix-seh-rev5

为系统(用户)变量 Path 添加 E:\MinGW-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\bin

使用 CMake 生成 OpenCV 的 Makefile

打开 cmake-gui,设置源码和生成路径:

  • Where is the source code: E:/opencv_341/opencv/sources
  • Where to build the binaries: E:/opencv_341/opencv_mingw64_build

点击 Configure,设置编译器

  • Specify the generator for this project: MinGW Makefiles
  • Specify native compilers
  • Next
  • Compilers C: E:\MinGW-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\bin\gcc.exe
  • Compilers C++: E:\MinGW-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\bin\g++.exe
  • Finish

编译配置:

  • 勾选 WITH_OPENGL
  • 勾选 ENABLE_CXX11
  • 不勾选 WITH_IPP
  • 不勾选 ENABLE_PRECOMPILED_HEADERS

点击 Configure,Generate 生成 Makefile

博主的配置信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
General configuration for OpenCV 3.4.1 =====================================
Version control: unknown

Platform:
Timestamp: 2018-07-31T02:14:11Z
Host: Windows 10.0.17134 AMD64
CMake: 3.12.0
CMake generator: MinGW Makefiles
CMake build tool: E:/MinGW-w64/x64-4.8.1-release-posix-seh-rev5/mingw64/bin/mingw32-make.exe
Configuration: Release

CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (3 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (9 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

C/C++:
Built as dynamic libs?: YES
C++11: YES
C++ Compiler: E:/MinGW-w64/x64-4.8.1-release-posix-seh-rev5/mingw64/bin/g++.exe (ver 4.8.1)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: E:/MinGW-w64/x64-4.8.1-release-posix-seh-rev5/mingw64/bin/gcc.exe
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,--gc-sections
Linker flags (Debug): -Wl,--gc-sections
ccache: NO
Precompiled headers: NO
Extra dependencies: opengl32 glu32
3rdparty dependencies:

OpenCV modules:
To be built: calib3d core dnn features2d flann highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python_bindings_generator shape stitching superres ts video videoio videostab
Disabled: js world
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: NO

Windows RT support: NO

GUI:
Win32 UI: YES
OpenGL support: YES (opengl32 glu32)
VTK support: NO

Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build (ver 90)
WEBP: build (ver encoder: 0x020e)
PNG: build (ver 1.6.34)
TIFF: build (ver 42 - 4.0.9)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 1.7.1)

Video I/O:
Video for Windows: YES
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (ver 57.107.100)
avformat: YES (ver 57.83.100)
avutil: YES (ver 55.78.100)
swscale: YES (ver 4.8.100)
avresample: YES (ver 3.7.0)
GStreamer: NO
DirectShow: YES

Parallel framework: none

Trace: YES (built-in)

Other third-party libraries:
Lapack: NO
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)

NVIDIA CUDA: NO

OpenCL: YES (no extra features)
Include path: E:/opencv_341/opencv/sources/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python (for build): E:/Python37-32/python.exe

Java:
ant: NO
JNI: C:/Program Files (x86)/Java/jdk1.8.0_181/include C:/Program Files (x86)/Java/jdk1.8.0_181/include/win32 C:/Program Files (x86)/Java/jdk1.8.0_181/include
Java wrappers: NO
Java tests: NO

Matlab: NO

Install to: E:/opencv_341/opencv_mingw64_build/install
-----------------------------------------------------------------

Configuring done
Generating done

编译 OpenCV

打开终端进行编译:(-j 是使用 8 个线程进行编译,请根据你的计算机配置合理设置线程数)

1
2
3
4
E:
cd E:\opencv_341\opencv_mingw64_build
mingw32-make -j 8
mingw32-make install

如果 mingw32-make -j 8 遇到错误,请看下面的 编译 OpenCV 常见错误,否则执行 mingw32-make install,完成安装。

编译 OpenCV 常见错误

1. MinGW-w64 的 aviriff.h 文件注释错误

表现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[ 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

MinGW-w64_aviriff.h_file_annotation_error

解决

打开E:\MinGW-w64\x64-4.8.1-release-posix-seh-rev5\mingw64\x86_64-w64-mingw32\include\aviriff.h

发现第一行的多行注释少了个/符号,加上保存,如下:

1
2
3
4
/**
* 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 就好了。

其他错误

修电脑的裁缝酱 wechat
扫描微信公众号关注我