OpenCV使用CMake和MinGW的编译 常见问题见最后
具体参照:https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
前言
本篇文章是介绍使用32在GW在Windows下编译OpenCV 生成32位的DLL
关于使用 64 位的 MinGW 编译 OpenCV 生成 64 位的 dll,后面更
软件环境
- Windows-10-64bit
-
Qt5.11.0
-
cmake-3.13.0-rc3-win64-x64
-
opencv-3.4.1
* Windows-10-64bit
* Qt5.11.0
* cmake-3.13.0-rc3-win64-x64
* opencv-3.4.1
```
opencv的MSVC及MinGW版
MSVC版
下载的 OpenCV 文件夹会有:
```shell
build (已编译好的库)
sources (源码)
</code></pre>
<pre><code class="language-shell line-numbers">Qt-Qt5.9-MingGW 5.3.0 32 bit
Qt-Tools-MinGW 5.3.0
</code></pre>
<p>**使用 MSVC 的话,直接在
build/x64/vc14
**使用 MSVC 的话,直接在
build/x64/vc14
里面就有了,配置好路径就可以使用。
MinGW 版
OpenCV 没有为我们编译好 MinGW 版,所以我们只能自己编译,下面就是介绍 MinGW 版的编译流程。
安装及配置 Qt、MinGW、CMake
安装
CMake 自行安装,Qt 和 MinGW 可以直接使用qt-opensource-windows-x86-5.9.3.exe 安装包安装,注意选择安装的组件(components)的时候勾选 MinGW :
Qt-Qt5.9-MingGW 5.3.0 32 bit
Qt-Tools-MinGW 5.3.0
配置 Qt、MinGW
安装好后打开QtCreator,在工具-选项-构建和运行-构建套件,选中Desktop Qt 5.9.3 MinGW 32bit,设为默认,OK。
添加 MinGW 到环境变量
为系统变量 Path 添加 E:\Qt\Qt5.9.3\Tools\mingw530_32\bin
使用 CMake 生成 OpenCV 的 Makefile
打开 cmake-gui,设置源码和生成路径:
<pre><code class="language-shell line-numbers">Where is the source code: E:/OpenCV_3.3.1/opencv/sources
Where to build the binaries: E:/OpenCV_3.3.1/opencv-build
</code></pre>
点击 Configure,设置编译器
<pre><code class="language-shell line-numbers">Specify the generator for this project: MinGW Makefiles
Specify native compilers
Next
Compilers C: E:\Qt\Qt5.9.3\Tools\mingw530_32\bin\gcc.exe
Compilers C++: E:\Qt\Qt5.9.3\Tools\mingw530_32\bin\g++.exe
Finish
</code></pre>
编译配置:
<pre><code class="language-shell line-numbers">勾选 WITH_QT
勾选 WITH_OPENGL
不勾选 WITH_IPP
</code></pre>
点击 Configure,再次配置:
<pre><code class="language-shell line-numbers"><br />设置 QT_MAKE_EXECUTABLE 为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\bin\qmake.exe
设置 Qt5Concurrent_DIR 为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Concurrent
设置 Qt5Core_DIR 为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Core
设置 Qt5Gui_DIR 为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Gui
设置 Qt5Test_DIR 为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Test
设置 Qt5Widgets_DIR 为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5Widgets
设置 Qt5OpenGL_DIR 为 E:\Qt\Qt5.9.3\5.9.3\mingw53_32\lib\cmake\Qt5OpenGL
设置 CMAKE_BUILD_TYPE 为 Release 或者 RelWithDebInfo
点击 Generate 生成 Makefile
OpenCVCMakeGenerate
编译 OpenCV
</code></pre>
打开终端进行编译:(-j 是使用 8 个线程进行编译,请根据你的计算机配置合理设置线程数)
cd E:\OpenCV_3.3.1\opencv-build
mingw32-make -j 8
mingw32-make install
编译 OpenCV 常见错误
<ol>
<li>多线程编译错误信息不明确,表现如下</li>
</ol>
<pre><code class="language-shell line-numbers"> 如果使用了多线程编译,导致错误,但是错误信息不明确,如:
```
```
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
MakeOpenCV_cap_dshow
```
解决方法
使用单线程编译:
mingw32-make
以查看详细的错误提示,再根据具体情况解决。
1. RC 错误
表现
... windres.exe: unknown option -- W ...
或者
```shell
FORMAT is one of rc, res, or coff, and is deduced from the file name
extension if not specified. A single file name is an input file.
No input-file is stdin, default rc. No output-file is stdout, default rc.
MakeOpenCVRCError
解决
在 cmake-gui 编译配置中:
不勾选 ENABLE_PRECOMPILED_HEADERS
然后重新Configure-Generate-mingw32-make
- sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA 错误
表现
...opencv/sources/modules/videoio/src/cap_dshow.cpp...
... 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...
或者
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
MakeOpenCV_cap_dshow
解决
修改E:\OpenCV_3.3.1\opencv\sources\modules\videoio\src\cap_dshow.cpp文件,在#include “DShow.h”这行的上面加一行#define NO_DSHOW_STRSAFE,如:
define NO_DSHOW_STRSAFE
include "DShow.h"
然后重新Configure-Generate-mingw32-make
- identifier ‘nullptr’ is a keyword in C++11 错误【2018年3月2日编译OpenCV 3.4.1时遇到并解决】
表现
D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:3: warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]
return s == nullptr || *s == 0;
^
D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc: In function 'bool google::protobuf::internal::win32::{anonymous}::null_or_empty(const char_type*)':
D:\opencv-3.4.1\opencv-3.4.1\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:94:15: error: 'nullptr' was not declared in this scope
return s == nullptr || *s == 0;
^
3rdparty\protobuf\CMakeFiles\libprotobuf.dir\build.make:412: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj' failed
mingw32-make[2]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj] Error 1
CMakeFiles\Makefile2:710: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all' failed
mingw32-make[1]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all] Error 2
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
MakeOpenCV_nullptr_Error
解决
在 cmake-gui 编译配置中:
勾选 ENABLE_CXX11
然后重新Configure-Generate-mingw32-make
编译完成后,添加 OpenCV 编译的库到环境变量
为系统变量 Path 添加 E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin
好了,大功告成!
————————————————
版权声明:本文为CSDN博主「lx0307」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/lx0307/article/details/84838422