Mingw32编译opencv库

文章目录

  • 1. 准备工作
  • 2. 编译
    • cmake构建程序
    • mingw32-make编译
  • 3. 安装
  • 4. 安装完的结果

注意:
mingw32-make编译的库和MSVC编译的库不兼容,MSVC和mingw-make生成的动态库使用的是不同的ABI(Application Binary Interface),不能混合使用由这两个编译器生成的库。例如,如果你的程序使用了由MSVC编译的库,那么你的程序也必须使用MSVC来编译。另外mingw32-make编译的库的库文件是.a后缀,MSVC编译的库的库文件是.lib。

1. 准备工作

  • 安装cmake
    参考

  • 安装mingw32
    参考

  • 下载opencv源码
    下载地址:https://codeload.github.com/opencv/opencv/zip/refs/tags/4.6.0
    下载后解压。

2. 编译

cmake构建程序

  • 进入opencv源码目录
  • 新建build目录
  • 进入build目录
  • 执行cmake命令
D:\myApp\opencv460\opencv-4.6.0>mkdir build


D:\myApp\opencv460\opencv-4.6.0>cd build


D:\myApp\opencv460\opencv-4.6.0\build>cmake .. -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=D:\myApp\opencv460\opencv-4.6.0\build -D BUILD_opencv_world=ON
-- The CXX compiler identification is GNU 13.2.0
-- The C compiler identification is GNU 13.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/myApp/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/myApp/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- ocv_init_download: OpenCV source tree is not fetched as git repository. 3rdparty resources will be downloaded from github.com by default.
-- Detected processor: AMD64
CMake Warning (dev) at cmake/OpenCVUtils.cmake:144 (find_package):
  Policy CMP0148 is not set: The FindPythonInterp and FindPythonLibs modules
  are removed.  Run "cmake --help-policy CMP0148" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning.

Call Stack (most recent call first):
  cmake/OpenCVDetectPython.cmake:64 (find_host_package)
  cmake/OpenCVDetectPython.cmake:271 (find_python)
  CMakeLists.txt:628 (include)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found PythonInterp: D:/myApp/anaconda3/python.exe (found suitable version "3.11.5", minimum required is "2.7")
CMake Warning at cmake/OpenCVDetectPython.cmake:81 (message):
  CMake's 'find_host_package(PythonInterp 2.7)' found wrong Python version:

  PYTHON_EXECUTABLE=D:/myApp/anaconda3/python.exe

  PYTHON_VERSION_STRING=3.11.5

  Consider providing the 'PYTHON2_EXECUTABLE' variable via CMake command line
  or environment variables

Call Stack (most recent call first):
  cmake/OpenCVDetectPython.cmake:271 (find_python)
  CMakeLists.txt:628 (include)
  。。。。。。。。。。。。。。。。。。。。

上面关键的指令是这一句:

cmake .. -G "MinGW Makefiles" -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=D:\myApp\opencv460\opencv-4.6.0\build -D BUILD_opencv_world=ON

解释一下:

  • … 表示上一级目录,即opencv源码目录
  • -G “MinGW Makefiles” 表示生成MinGW Makefiles工程
  • -D CMAKE_BUILD_TYPE=RELEASE 表示编译类型为RELEASE
  • -D CMAKE_INSTALL_PREFIX=D:\myApp\opencv460\opencv-4.6.0\build 表示安装目录
  • -D BUILD_opencv_world=ON 表示编译opencv_world库

mingw32-make编译

  • 执行mingw32-make命令
mingw32-make -j8

输出如下:

D:\myApp\opencv460\opencv-4.6.0\build>mingw32-make -j 8
[  0%] Built target opencv_videoio_plugins
[  0%] Building C object 3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/thread.c.obj
[  0%] Built target opencv_highgui_plugins
[  0%] Building CXX object CMakeFiles/ade.dir/3rdparty/ade/ade-0.1.1f/sources/ade/source/alloc.cpp.obj
[  0%] Building C object 3rdparty/quirc/CMakeFiles/quirc.dir/src/decode.c.obj
[  0%] Building C object 3rdparty/zlib/CMakeFiles/zlib.dir/adler32.c.obj
[  0%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcapimin.c.obj
[  0%] Building C object 3rdparty/libwebp/CMakeFiles/libwebp.dir/src/dec/alpha_dec.c.obj
[  0%] Building C object 3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/bio.c.obj
[  0%] Building C object 3rdparty/quirc/CMakeFiles/quirc.dir/src/quirc.c.obj

note: -j8 表示8个线程编译,可以根据自己的电脑配置来设置。
如果报错如下

D:/myApp/opencv460/opencv-4.6.0/build/3rdparty/ade/ade-0.1.1f/sources/ade/include/ade/typed_graph.hpp:101:10: error:
'uintptr_t' in namespace 'std' does not name a type
  101 |     std::uintptr_t m_srcGraph;
      |          ^~~~~~~~~
D:/myApp/opencv460/opencv-4.6.0/build/3rdparty/ade/ade-0.1.1f/sources/ade/include/ade/typed_graph.hpp:22:1: note: 'std::uintptr_t' is defined in header ''; did you forget to '#include '?
   21 | #include "typed_metadata.hpp"
  +++ |+#include 

这是因为ade库用到了std::uintptr_t,std::uintptr_t在cstdint头文件中。但是它没有包含cstdint头文件,需要手动添加。(编译报错的提示还是很有用的)

3. 安装

mingw32-make install
D:\myApp\opencv460\opencv-4.6.0\build>mingw32-make install
[  0%] Built target opencv_highgui_plugins
[  2%] Built target libopenjp2
[  2%] Built target opencv_videoio_plugins
[  3%] Built target zlib
[  9%] Built target opencv_core
[ 15%] Built target opencv_imgproc
[ 18%] Built target libjpeg-turbo
[ 25%] Built target libwebp
[ 28%] Built target libtiff
[ 29%] Built target libpng
[ 35%] Built target IlmImf
[ 36%] Built target opencv_imgcodecs
[ 37%] Built target opencv_videoio
[ 37%] Built target opencv_highgui
[ 37%] Built target opencv_ts
[ 40%] Built target opencv_test_core
[ 42%] Built target opencv_perf_core
[ 42%] Built target opencv_flann
[ 42%] Built target opencv_test_flann
[ 46%] Built target opencv_test_imgproc
[ 48%] Built target opencv_perf_imgproc
[ 49%] Built target opencv_ml
[ 50%] Built target opencv_test_ml
[ 51%] Built target opencv_photo
[ 52%] Built target opencv_test_photo
[ 53%] Built target opencv_perf_photo
[ 55%] Built target libprotobuf
[ 64%] Built target opencv_dnn
[ 65%] Built target opencv_test_dnn
[ 65%] Built target opencv_perf_dnn
[ 67%] Built target opencv_features2d
[ 68%] Built target opencv_test_features2d
[ 69%] Built target opencv_perf_features2d
[ 69%] Built target opencv_test_imgcodecs
[ 69%] Built target opencv_perf_imgcodecs
[ 70%] Built target opencv_test_videoio
[ 70%] Built target opencv_perf_videoio
[ 73%] Built target opencv_calib3d
[ 75%] Built target opencv_test_calib3d
[ 76%] Built target opencv_perf_calib3d
[ 76%] Built target opencv_test_highgui
[ 77%] Built target quirc
[ 78%] Built target opencv_objdetect
[ 78%] Built target opencv_test_objdetect
[ 78%] Built target opencv_perf_objdetect
[ 79%] Built target opencv_stitching
[ 79%] Built target opencv_test_stitching
[ 79%] Built target opencv_perf_stitching
[ 80%] Built target opencv_video
[ 81%] Built target opencv_test_video
[ 82%] Built target opencv_perf_video
[ 83%] Built target ade
[ 91%] Built target opencv_gapi
[ 97%] Built target opencv_test_gapi
[ 98%] Built target opencv_perf_gapi
[ 98%] Built target gen_opencv_python_source
[ 99%] Built target opencv_python3
[ 99%] Built target opencv_annotation
[ 99%] Built target opencv_visualisation
[ 99%] Built target opencv_interactive-calibration
[100%] Built target opencv_version
[100%] Built target opencv_version_win32
[100%] Built target opencv_model_diagnostics
Install the project...
-- Install configuration: "Release"
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/opencl-headers-LICENSE.txt
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/ade-LICENSE
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/ffmpeg-license.txt
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/ffmpeg-readme.txt
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/include/opencv2/cvconfig.h
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/include/opencv2/opencv_modules.hpp
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/x64/mingw/lib/OpenCVModules.cmake
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/x64/mingw/lib/OpenCVModules-release.cmake
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/x64/mingw/lib/OpenCVConfig-version.cmake
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/x64/mingw/lib/OpenCVConfig.cmake
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/./OpenCVConfig-version.cmake
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/./OpenCVConfig.cmake
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/./LICENSE
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/./setup_vars_opencv4.cmd
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/zlib-README
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libjpeg-turbo-README.md
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libjpeg-turbo-LICENSE.md
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libjpeg-turbo-README.ijg
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libtiff-COPYRIGHT
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libopenjp2-README.md
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libopenjp2-LICENSE
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libpng-LICENSE
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/libpng-README
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/openexr-LICENSE
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/openexr-AUTHORS.ilmbase
-- Installing: D:/myApp/opencv460/opencv-4.6.0/build/install/etc/licenses/openexr-AUTHORS.openexr

4. 安装完的结果

Mingw32编译opencv库_第1张图片

你可能感兴趣的:(opencv,opencv,计算机视觉,mingw32,windows)