本篇博文是使用 32 位的 MinGW 在 Windows 下編譯 OpenCV 生成 32 位的 dll。
關於使用 64 位的 MinGW 編譯 OpenCV 生成 64 位的 dll,見:OpenCV使用CMake和MinGW-w64的編譯安裝
編譯好的 OpenCV(MinGW 版):
Github . huihut/OpenCV-MinGW-Build
軟件環境
- Windows-10-64bit
- Qt-5.9.3
- MinGW-5.3.0-32bit
- CMake-3.9.2
- OpenCV-3.3.1 / 3.4.1(適用)
OpenCV 的 MSVC 版及 MinGW 版
MSVC 版
下載的 OpenCV 文件夾會有:
- build (已編譯好的庫)
- sources (源碼)
使用 MSVC 的話,直接在
build/x64/vc14
裏面就有了,配置好路徑就可以使用。
MinGW 版
OpenCV 沒有爲我們編譯好 MinGW 版,所以我們只能自己編譯,下面就是介紹 MinGW 版的編譯流程。
也可以直接下載使用我編譯好了的 OpenCV (在上文)。
安裝及配置 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,設置源碼和生成路徑:
- Where is the source code: E:/OpenCV_3.3.1/opencv/sources
- Where to build the binaries: E:/OpenCV_3.3.1/opencv-build
點擊 Configure,設置編譯器
- 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
編譯配置:
- 勾選
WITH_QT
- 勾選
WITH_OPENGL
點擊 Configure,再次配置:
- 不勾選
WITH_IPP
- 設置
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
編譯 OpenCV
打開終端進行編譯:(-j
是使用 8
個線程進行編譯,請根據你的計算機配置合理設置線程數)
E:
cd E:\OpenCV_3.3.1\opencv-build
mingw32-make -j 8
mingw32-make install
如果 mingw32-make -j 8
遇到錯誤,請看下面的 編譯 OpenCV 常見錯誤,否則執行 mingw32-make install
,完成安裝。
編譯 OpenCV 常見錯誤
0. 多線程編譯錯誤信息不明確
表現
如果使用了多線程編譯,導致錯誤,但是錯誤信息不明確,如:
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
解決
使用單線程編譯:
mingw32-make
以查看詳細的錯誤提示,再根據具體情況解決。
1. RC 錯誤
表現
... windres.exe: unknown option -- W ...
或者
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.
解決
在 cmake-gui 編譯配置中:
- 不勾選
ENABLE_PRECOMPILED_HEADERS
然後重新Configure
-Generate
-mingw32-make
2. 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
解決
修改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
3. 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
解決
在 cmake-gui 編譯配置中:
- 勾選
ENABLE_CXX11
然後重新Configure
-Generate
-mingw32-make
添加 OpenCV 編譯的庫到環境變量
- 爲系統變量
Path
添加E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin
新建 OpenCV 的 Qt 項目
在 .pro
文件裏面添加:
win32 {
INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*.dll
}
或者:(區分 debug 和 release 是因爲 OpenCV 對其兩者有不同的庫,你需要把路徑改爲你自己的,我編譯 MinGW 的 OpenCV 只有 release 庫)
win32 {
INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
CONFIG(debug, debug|release): {
LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*d.dll
} else:CONFIG(release, debug|release): {
LIBS += -LE:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin \
-llibopencv_core331 \
-llibopencv_highgui331 \
-llibopencv_imgcodecs331 \
-llibopencv_imgproc331 \
-llibopencv_features2d331 \
-llibopencv_calib3d331
}
}
然後在 MainWindow 中如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// read an image
cv::Mat image = cv::imread("E:/Pictures/H_white.png", 1);
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
最後運行起來了,效果如圖:
官方教程
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-df60374684.css" rel="stylesheet">
</div>
<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod=popu_307 data-dsm = "post" >
<div class="article-copyright">
<svg class="icon" title="CSDN認證原創" aria-hidden="true" style="width:53px; height: 18px; vertical-align: -4px;">
<use xlink:href="#CSDN_Cert"></use>
</svg>
版權聲明:本文由 輝哈 原創,博客鏈接爲 http://blog.csdn.net/huihut ,轉載請註明出處。 https://blog.csdn.net/huihut/article/details/78701814 </div>
<div id="content_views" class="markdown_views prism-github-gist">
<!-- flowchart 箭頭圖標 勿刪 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
<h2><a id="_1"></a>前言</h2>
本篇博文是使用 32 位的 MinGW 在 Windows 下編譯 OpenCV 生成 32 位的 dll。
關於使用 64 位的 MinGW 編譯 OpenCV 生成 64 位的 dll,見:OpenCV使用CMake和MinGW-w64的編譯安裝
編譯好的 OpenCV(MinGW 版):
Github . huihut/OpenCV-MinGW-Build
軟件環境
- Windows-10-64bit
- Qt-5.9.3
- MinGW-5.3.0-32bit
- CMake-3.9.2
- OpenCV-3.3.1 / 3.4.1(適用)
OpenCV 的 MSVC 版及 MinGW 版
MSVC 版
下載的 OpenCV 文件夾會有:
- build (已編譯好的庫)
- sources (源碼)
使用 MSVC 的話,直接在
build/x64/vc14
裏面就有了,配置好路徑就可以使用。
MinGW 版
OpenCV 沒有爲我們編譯好 MinGW 版,所以我們只能自己編譯,下面就是介紹 MinGW 版的編譯流程。
也可以直接下載使用我編譯好了的 OpenCV (在上文)。
安裝及配置 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,設置源碼和生成路徑:
- Where is the source code: E:/OpenCV_3.3.1/opencv/sources
- Where to build the binaries: E:/OpenCV_3.3.1/opencv-build
點擊 Configure,設置編譯器
- 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
編譯配置:
- 勾選
WITH_QT
- 勾選
WITH_OPENGL
點擊 Configure,再次配置:
- 不勾選
WITH_IPP
- 設置
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
編譯 OpenCV
打開終端進行編譯:(-j
是使用 8
個線程進行編譯,請根據你的計算機配置合理設置線程數)
E:
cd E:\OpenCV_3.3.1\opencv-build
mingw32-make -j 8
mingw32-make install
如果 mingw32-make -j 8
遇到錯誤,請看下面的 編譯 OpenCV 常見錯誤,否則執行 mingw32-make install
,完成安裝。
編譯 OpenCV 常見錯誤
0. 多線程編譯錯誤信息不明確
表現
如果使用了多線程編譯,導致錯誤,但是錯誤信息不明確,如:
Makefile:161: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
解決
使用單線程編譯:
mingw32-make
以查看詳細的錯誤提示,再根據具體情況解決。
1. RC 錯誤
表現
... windres.exe: unknown option -- W ...
或者
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.
解決
在 cmake-gui 編譯配置中:
- 不勾選
ENABLE_PRECOMPILED_HEADERS
然後重新Configure
-Generate
-mingw32-make
2. 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
解決
修改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
3. 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
解決
在 cmake-gui 編譯配置中:
- 勾選
ENABLE_CXX11
然後重新Configure
-Generate
-mingw32-make
添加 OpenCV 編譯的庫到環境變量
- 爲系統變量
Path
添加E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin
新建 OpenCV 的 Qt 項目
在 .pro
文件裏面添加:
win32 {
INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*.dll
}
或者:(區分 debug 和 release 是因爲 OpenCV 對其兩者有不同的庫,你需要把路徑改爲你自己的,我編譯 MinGW 的 OpenCV 只有 release 庫)
win32 {
INCLUDEPATH += E:\OpenCV_3.3.1\opencv-build\install\include
CONFIG(debug, debug|release): {
LIBS += E:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin\libopencv_*d.dll
} else:CONFIG(release, debug|release): {
LIBS += -LE:\OpenCV_3.3.1\opencv-build\install\x86\mingw\bin \
-llibopencv_core331 \
-llibopencv_highgui331 \
-llibopencv_imgcodecs331 \
-llibopencv_imgproc331 \
-llibopencv_features2d331 \
-llibopencv_calib3d331
}
}
然後在 MainWindow 中如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// read an image
cv::Mat image = cv::imread("E:/Pictures/H_white.png", 1);
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
最後運行起來了,效果如圖:
官方教程
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-df60374684.css" rel="stylesheet">
</div>