侧边栏壁纸
  • 累计撰写 88 篇文章
  • 累计创建 41 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录

Windows + CMake + MinGW 编译安装 OpenCV 过程记录

勤为径苦作舟
2023-07-17 / 0 评论 / 0 点赞 / 45 阅读 / 0 字

简介

此文是过程记录,无需安装 Visual Studio 或 Clion。

MinGW 安装配置

GNU 编译器套件,用于在 Windows 中编译 C/C++ 程序。CMake 编译 OpenCV 时需要用到。

下载地址:MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net

下载x86_64-win32-seh并解压到软件目录。

系统环境变量新增MINGW_HOMED:\ProgramFiles\mingw64PATH系统变量追加%MINGW_HOME%\bin%MINGW_HOME%\lib%MINGW_HOME%\include

命令行gcc -v查看安装配置是否成功:

OpenCV 源码下载

下载地址:Releases - OpenCV

下载后解压,在opencv目录中新建build_mingw文件夹。

安装 CMake 并编译 OpenCV

下载地址:Download | CMake

安装时选择将安装目录添加到系统环境变量。

安装后打开CMake (cmake-gui),选择Where is the source codeWhere to build the binaries后,点击Configure


报错提示:

'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)
CMake Error at CMakeLists.txt:113 (enable_language):
  Running

   'nmake' '-?'

  failed with:

   系统找不到指定的文件。


CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
Configuring incomplete, errors occurred!

一是说没有设置CMAKE_C_COMPILERCMAKE_CXX_COMPILER,二是说找不到nmake。但即使我们Add Entry了这两项,Configure仍报错,需要在启动 CMake 时使用-G指定生成器才行。

在 OpenCV 的解压目录中,先删除build_mingw文件夹,再运行命令:

cmake -S sources -B build_mingw -G "MinGW Makefiles"

cd build_mingw

mingw32-make

注意:不确定是否必须要安装 JavaPython,如有提示自行安装。

mingw32-make报 Mutex 相关错误:

D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:718:14: error: 'recursive_mutex' in namespace 'std' does not name a type
 typedef std::recursive_mutex Mutex;
              ^~~~~~~~~~~~~~~
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:718:9: note: 'std::recursive_mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:63:1:
+#include <mutex>
 //#endif
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:718:9:
 typedef std::recursive_mutex Mutex;
         ^~~
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:719:29: error: 'Mutex' is not a member of
cv'
 typedef std::lock_guard<cv::Mutex> AutoLock;
                             ^~~~~
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:719:29: note: suggested alternative: 'Matx'
 typedef std::lock_guard<cv::Mutex> AutoLock;
                             ^~~~~
                             Matx
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:719:29: error: 'Mutex' is not a member of
cv'
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:719:29: note: suggested alternative: 'Matx'
 typedef std::lock_guard<cv::Mutex> AutoLock;
                             ^~~~~
                             Matx
D:/ProgramFiles/opencv/sources/modules/core/include/opencv2/core/utility.hpp:719:34: error: template argument 1 is invalid
 typedef std::lock_guard<cv::Mutex> AutoLock;
                                  ^
In file included from D:\ProgramFiles\opencv\sources\modules\core\src\algorithm.cpp:43:
D:\ProgramFiles\opencv\sources\modules\core\src\precomp.hpp:393:5: error: 'Mutex' in namespace 'cv' does not name a type
 cv::Mutex& getInitializationMutex();
     ^~~~~
D:\ProgramFiles\opencv\sources\modules\core\src\precomp.hpp:393:1: note: suggested alternative: 'Matx'
 cv::Mutex& getInitializationMutex();
 ^~
 Matx
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:103: modules/core/CMakeFiles/opencv_core.dir/src/algorithm.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:1949: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
mingw32-make: *** [Makefile:165: all] Error 2

需要下载 POSIX 版本的 MinGW,下载解压替换掉原来的。

下载地址:Releases · niXman/mingw-builds-binaries

删掉 OpenCV 的解压目录下的build_mingw,重复之前的命令。

漫长的等待之后,编译到 35% 的时候报错:

D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IMFAttributes>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IMFAttributes>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IMFAttributes>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)()'}
  106 |     _COM_SMARTPTR_TYPEDEF(T, __uuidof(T));
      |                           ^
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp: In instantiation of 'class cv::obsensor::ComPtr<IMFMediaSource>':
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:146:44:   required from here
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IMFMediaSource>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IMFMediaSource>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IMFMediaSource>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)()'}
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp: In instantiation of 'class cv::obsensor::ComPtr<IMFSourceReader>':
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:148:45:   required from here
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IMFSourceReader>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IMFSourceReader>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IMFSourceReader>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)()'}
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp: In instantiation of 'class cv::obsensor::ComPtr<IAMCameraControl>':
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:149:47:   required from here
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IAMCameraControl>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IAMCameraControl>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IAMCameraControl>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)()'}
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp: In instantiation of 'class cv::obsensor::ComPtr<IAMVideoProcAmp>':
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:150:45:   required from here
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IAMVideoProcAmp>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IAMVideoProcAmp>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IAMVideoProcAmp>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)()'}
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp: In instantiation of 'class cv::obsensor::ComPtr<IKsTopologyInfo>':
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:151:49:   required from here
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IKsTopologyInfo>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IKsTopologyInfo>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IKsTopologyInfo>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)()'}
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp: In instantiation of 'class cv::obsensor::ComPtr<IUnknown>':
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:152:40:   required from here
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IUnknown>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IUnknown>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IUnknown>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)()'}
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp: In instantiation of 'class cv::obsensor::ComPtr<IKsControl>':
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:153:39:   required from here
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:106:27: error: could not convert template argument 'cv::obsensor::ComPtr<IKsControl>::__T_IID_getter' from 'const IID& (cv::obsensor::ComPtr<IKsControl>::)()' {aka 'const GUID& (cv::obsensor::ComPtr<IKsControl>::)()'} to 'const IID& (*)()' {aka 'const GUID& (*)(
'}
D:\ProgramFiles\opencv\sources\modules\videoio\src\cap_obsensor\obsensor_stream_channel_msmf.hpp:129:7: warning: base class 'struct IMFSourceReaderCallback' has accessible non-virtual destructor [-Wnon-virtual-dtor]
  129 | class MSMFStreamChannel : public IUvcStreamChannel, public IMFSourceReaderCallback
      |       ^~~~~~~~~~~~~~~~~
mingw32-make[2]: *** [modules\videoio\CMakeFiles\opencv_videoio.dir\build.make:241: modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_obsensor/obsensor_uvc_stream_channel.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:2644: modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
mingw32-make: *** [Makefile:165: all] Error 2

原因是Obsensor(传感器数据采集框架)和 GCC 不兼容,打开build_mingw下的CMakeCache.txt,将其中的WITH_OBSENSOR:BOOL=ON改成WITH_OBSENSOR:BOOL=OFF,重新执行mingw32-make命令。

编译成功之后再运行mingw32-make install命令。
系统环境变量新增OPENCV_HOMED:\ProgramFiles\opencvPATH系统变量追加%OPENCV_HOME%\build_mingw\install\x64\mingw\bin

参考

0

评论区