当前位置:   article > 正文

多平台编译OpenCV 4.6.0源码操作步骤_opencv4.6 python源码

opencv4.6 python源码

      从https://github.com/opencv/opencv/releases/tag/4.6.0 下载源码opencv-4.6.0.tar.gz,解压缩。

      多平台包括Windows 10、Linux(Ubuntu 16.04、Ubuntu 18.04)、MacOS、Android、iOS、Wasm(WebAssembly)、嵌入式设备,以及它们不同的配置,如Ubuntu上不同的gcc版本,MacOS上不同的xcode版本。
      将不同平台的编译命令全部写在一个shell脚本build.sh里,此脚本与OpenCV的主CMakeLists.txt在同一个目录下,外层通过输入一个参数来控制编译哪个平台哪个配置的库,如linux-x86_64-gcc5_4,命名组成方式为:platform-arch-toolchain,platform可为winodws、linux、osx(即MacOS)、andorid、ios、wasm;arch可为x86、x86_64、armv7、aarch64(arm64);toolchain可为gcc5.4、gcc8.2、clang-r23b、vs2015等。
      编译前的说明:
      (1).除特殊平台如Wasm、iOS只有静态库外,其它平台既包括动态库也包括静态库。
      (2).除Windows外,其它平台只有Release库。
      (3).为保证调用OpenCV接口时各平台结果的一致性,这里图像编解码库如libpng等不使用系统库中的,均使用OpenCV中自带的版本即3rdparty目录下的。
      (4).为保证各平台结果一致(精度对齐),也关闭了OpenCV中的加速选项,如SIMD、OpenMP等,尽量只走普通C++实现部分。
      (5).关闭了多线程:即设置WITH_PTHREADS_PF为OFF,设置OPENCV_DISABLE_THREAD_SUPPORT为ON后,当通过VideoCapture读取视频文件时,会有Warning:Corrupt JPEG data: premature end of data segment
      (6).有些OpenCV的选项在多平台上是通用的,这里将它们放在了一起,如下:其中将外部的输入参数传给了PLATFORM_ARCH_TOOLCHAIN,用于不同的平台在CMakeList.txt中做的差异处理,设置OPENCV_DISABLE_THREAD_SUPPORT为ON,将不会编译objdetect模块

  1. common_options="-DCMAKE_BUILD_TYPE=RELEASE \
  2. -DPLATFORM_ARCH_TOOLCHAIN=$1 \
  3. -DCMAKE_CXX_FLAGS=-fPIC \
  4. -DCMAKE_C_FLAGS=-fPIC \
  5. -DBUILD_EXAMPLES=OFF \
  6. -DBUILD_opencv_apps=OFF \
  7. -DBUILD_PERF_TESTS=OFF \
  8. -DBUILD_TESTS=OFF \
  9. -DBUILD_JAVA=OFF \
  10. -DBUILD_OBJC=OFF \
  11. -DBUILD_KOTLIN_EXTENSIONS=OFF \
  12. -DBUILD_opencv_gapi=OFF \
  13. -DOPENCV_FORCE_3RDPARTY_BUILD=ON \
  14. -DENABLE_PIC=ON \
  15. -DWITH_1394=OFF \
  16. -DWITH_EIGEN=OFF \
  17. -DWITH_ARAVIS=OFF \
  18. -DWITH_ARITH_DEC=ON \
  19. -DWITH_ARITH_ENC=ON \
  20. -DWITH_CLP=OFF \
  21. -DWITH_CUBLAS=OFF \
  22. -DWITH_CUDA=OFF \
  23. -DWITH_CUFFT=OFF \
  24. -DWITH_FFMPEG=OFF \
  25. -DWITH_GSTREAMER=OFF \
  26. -DWITH_GSTREAMER_0_10=OFF \
  27. -DWITH_HALIDE=OFF \
  28. -DWITH_HPX=OFF \
  29. -DWITH_IMGCODEC_HDR=ON \
  30. -DWITH_IMGCODEC_PXM=ON \
  31. -DWITH_IMGCODEC_SUNRASTER=ON \
  32. -DWITH_INF_ENGINE=OFF \
  33. -DWITH_IPP=OFF \
  34. -DWITH_ITT=OFF \
  35. -DWITH_OPENVINO=OFF \
  36. -DWITH_JASPER=ON \
  37. -DWITH_JPEG=ON \
  38. -DWITH_PNG=ON \
  39. -DWITH_TIFF=ON \
  40. -DWITH_WEBP=ON \
  41. -DWITH_LAPACK=OFF \
  42. -DWITH_PTHREADS_PF=OFF \
  43. -DOPENCV_DISABLE_THREAD_SUPPORT=ON \
  44. -DWITH_LIBREALSENSE=OFF \
  45. -DWITH_MSMF_DXVA=OFF \
  46. -DWITH_NVCUVID=OFF \
  47. -DWITH_OPENCL=OFF \
  48. -DWITH_OPENCLAMDBLAS=OFF \
  49. -DWITH_OPENCLAMDFFT=OFF \
  50. -DWITH_OPENCL_SVM=OFF \
  51. -DWITH_OPENCL_D3D11_NV=OFF \
  52. -DWITH_VA=OFF \
  53. -DWITH_VA_INTEL=OFF \
  54. -DWITH_OPENEXR=OFF \
  55. -DWITH_OPENGL=OFF \
  56. -DWITH_OPENMP=OFF \
  57. -DWITH_OPENNNI=OFF \
  58. -DWITH_OPENNNI2=OFF \
  59. -DWITH_OPENVX=OFF \
  60. -DWITH_PROTOBUF=OFF \
  61. -DWITH_PVAPI=OFF \
  62. -DWITH_QT=OFF \
  63. -DWITH_QUIRC=OFF \
  64. -DWITH_TBB=OFF \
  65. -DWITH_VULKAN=OFF \
  66. -DWITH_XIMEA=OFF \
  67. -DWITH_VTK=OFF \
  68. -DWITH_CPUFEATURES=OFF \
  69. -DENABLE_NEON=OFF \
  70. -DCPU_BASELINE=OFF \
  71. -DCV_ENABLE_INTRINSICS=OFF \
  72. -DCV_DISABLE_OPTIMIZATION=ON \
  73. -DINSTALL_C_EXAMPLES=OFF \
  74. -DINSTALL_PYTHON_EXAMPLES=OFF"

      (7).因为编译的既有动态库又有静态库,这里定义了两个变量,如下:编译完的库存放在与build.sh同一目录下,动态库目录结构为install/$1/${dir_name_dynamic},静态库目录结构为install/$1/${dir_name_static}

  1. dir_name_dynamic="shared"
  2. dir_name_static="static"

      (8).因为会有很多不同的toolchain,它们存放在公共的目录下,如/usr/local/toolchains,这里定义了一个变量,用于指定公共目录,如下:

toolchains_path="/usr/local/toolchains"

      1. Linux:Ubuntu 16.04、Ubuntu 18.04
      调整源码:
      (1).因为是在linux 64位机上编译linux x86的库,调整主CMakeLists.txt文件,对CMAKE_C_FLAGS和CMAKE_CXX_FLAGS追加-m32选项。
      (2).用海思arm-himix100-linux工具链编译时,需调整主CMakeLists.txt文件,增加一个变量:add_definitions(-DLINUX_ARMV7_HIMIX100=1),此变量应用在后面介绍的extend_std_funcs.hpp文件中,否则编译时会error,类似于android gcc,如:error: 'cbrt' is not a member of 'std'。因为在海思arm-himix100-linux工具链中并没有找到仅在此工具链中适用的预定义宏,因此增加了LINUX_ARMV7_HIMIX100。在后面项目中使用此opencv库时,也需要在那个项目的CMakeLists.txt中添加:add_definitions(-DLINUX_ARMV7_HIMIX100=1)
      (3).用海思arm-himix200-linux工具链编译时,需调整主CMakeLists.txt文件,增加一个变量:add_definitions(-DLINUX_ARMV7_HIMIX200=1),此变量在modules/core/include/opencv2/core/detail/exception_ptr.hpp中使用,当定义此变量时,强制添加#undef CV__EXCEPTION_PTR,否则编译库时会报error,如error: ‘std::exception_ptr’ has not been declared。因为在海思arm-himix200-linux工具链中并没有找到仅在此工具链中适用的预定义宏,因此增加了LINUX_ARMV7_HIMIX200。在后面项目中使用此opencv库时,也需要在那个项目的CMakeLists.txt中添加:add_definitions(-DLINUX_ARMV7_HIMIX200=1)
      (4).使用QNX工具链编译时,在modules/calib3d/src/usac.hpp中,添加宏定义,如下,否则会报error,如:error: 'M_SQRT2' was not declared in this scope

  1. #ifdef __QNX__
  2. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  3. #define M_PI 3.14159265358979323846 /* pi */
  4. #endif

      编译:

      (1).编译linux-x86_64-gcc系列,如linux-x86_64-gcc5_4,则编译命令如下:

  1. if [ $1 == "linux-x86_64-gcc5_4" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=${toolchains_path}/gcc-5.4/bin/gcc \
  7. -DCMAKE_CXX_COMPILER=${toolchains_path}/gcc-5.4/bin/g++ \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  10. ..
  11. make -j8
  12. make install
  13. echo "#### build static libraries ..."
  14. rm -rf *
  15. cmake \
  16. ${common_options} \
  17. -DCMAKE_C_COMPILER=${toolchains_path}/gcc-5.4/bin/gcc \
  18. -DCMAKE_CXX_COMPILER=${toolchains_path}/gcc-5.4/bin/g++ \
  19. -DBUILD_SHARED_LIBS=OFF \
  20. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  21. ..
  22. make -j8
  23. make install

      (2).编译linux-x86-gcc4_9,编译命令如下:

  1. elif [ $1 == "linux-x86-gcc4_9" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=/usr/bin/gcc-4.9 \
  7. -DCMAKE_CXX_COMPILER=/usr/bin/g++-4.9 \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DWITH_GTK=OFF \
  10. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  11. ..
  12. make -j8
  13. make install
  14. echo "#### build static libraries ..."
  15. rm -rf *
  16. cmake \
  17. ${common_options} \
  18. -DCMAKE_C_COMPILER=/usr/bin/gcc-4.9 \
  19. -DCMAKE_CXX_COMPILER=/usr/bin/g++-4.9 \
  20. -DBUILD_SHARED_LIBS=OFF \
  21. -DWITH_GTK=OFF \
  22. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  23. ..
  24. make -j8
  25. make install

      (3).使用海思的工具链arm-himix100-linux,编译命令如下:

  1. elif [ $1 == "linux-armv7-himix100" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix100-linux/bin/arm-himix100-linux-gcc \
  7. -DCMAKE_CXX_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix100-linux/bin/arm-himix100-linux-g++ \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DWITH_GTK=OFF \
  10. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  11. ..
  12. make -j8
  13. make install
  14. echo "#### build static libraries ..."
  15. rm -rf *
  16. cmake \
  17. ${common_options} \
  18. -DCMAKE_C_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix100-linux/bin/arm-himix100-linux-gcc \
  19. -DCMAKE_CXX_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix100-linux/bin/arm-himix100-linux-g++ \
  20. -DBUILD_SHARED_LIBS=OFF \
  21. -DWITH_GTK=OFF \
  22. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  23. ..
  24. make -j8
  25. make install

      (4).使用海思的工具链arm-himix200-linux,编译命令如下:

  1. elif [ $1 == "linux-armv7-himix200" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-gcc \
  7. -DCMAKE_CXX_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-g++ \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DWITH_GTK=OFF \
  10. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  11. ..
  12. make -j8
  13. make install
  14. echo "#### build static libraries ..."
  15. rm -rf *
  16. cmake \
  17. ${common_options} \
  18. -DCMAKE_C_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-gcc \
  19. -DCMAKE_CXX_COMPILER=${toolchains_path}/hisi-linux/x86-arm/arm-himix200-linux/bin/arm-himix200-linux-g++ \
  20. -DBUILD_SHARED_LIBS=OFF \
  21. -DWITH_GTK=OFF \
  22. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  23. ..
  24. make -j8
  25. make install

      (5).使用海思的工具链aarch64-hi3559a-linux,编译命令如下:关闭了webp,否则会有error: "internal compiler erro"错误,解决此问题可能需要升级gcc版本

  1. elif [ $1 == "linux-aarch64-hi3559a" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=${toolchains_path}/hisi-linux/aarch64-hi3559a-linux/bin/aarch64-linux-gnu-gcc \
  7. -DCMAKE_CXX_COMPILER=${toolchains_path}/hisi-linux/aarch64-hi3559a-linux/bin/aarch64-linux-gnu-g++ \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DWITH_GTK=OFF \
  10. -DWITH_WEBP=OFF \
  11. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  12. ..
  13. make -j8
  14. make install
  15. echo "#### build static libraries ..."
  16. rm -rf *
  17. cmake \
  18. ${common_options} \
  19. -DCMAKE_C_COMPILER=${toolchains_path}/hisi-linux/aarch64-hi3559a-linux/bin/aarch64-linux-gnu-gcc \
  20. -DCMAKE_CXX_COMPILER=${toolchains_path}/hisi-linux/aarch64-hi3559a-linux/bin/aarch64-linux-gnu-g++ \
  21. -DBUILD_SHARED_LIBS=OFF \
  22. -DWITH_GTK=OFF \
  23. -DWITH_WEBP=OFF \
  24. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  25. ..
  26. make -j8
  27. make install

      (6).使用QNX工具链,编译命令如下:编译动态库时关闭OPENCV_ENABLE_MEMALIGN,关闭静态库时关闭OPENCV_ENABLE_MEMALIGN和WITH_ADE

  1. elif [ $1 == "linux-aarch64-qnx" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. source ${toolchains_path}/qnx700/qnxsdp-env.sh
  5. cmake \
  6. ${common_options} \
  7. -DCMAKE_C_COMPILER=${toolchains_path}/qnx700/host/linux/x86_64/usr/bin/aarch64-unknown-nto-qnx7.0.0-gcc \
  8. -DCMAKE_CXX_COMPILER=${toolchains_path}/qnx700/host/linux/x86_64/usr/bin/aarch64-unknown-nto-qnx7.0.0-g++ \
  9. -DBUILD_SHARED_LIBS=ON \
  10. -DWITH_GTK=OFF \
  11. -DOPENCV_ENABLE_MEMALIGN=OFF \
  12. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  13. ..
  14. make -j8
  15. make install
  16. echo "#### build static libraries ..."
  17. rm -rf *
  18. source ${toolchains_path}/qnx700/qnxsdp-env.sh
  19. cmake \
  20. ${common_options} \
  21. -DCMAKE_C_COMPILER=${toolchains_path}/qnx700/host/linux/x86_64/usr/bin/aarch64-unknown-nto-qnx7.0.0-gcc \
  22. -DCMAKE_CXX_COMPILER=${toolchains_path}/qnx700/host/linux/x86_64/usr/bin/aarch64-unknown-nto-qnx7.0.0-g++ \
  23. -DBUILD_SHARED_LIBS=OFF \
  24. -DWITH_GTK=OFF \
  25. -DOPENCV_ENABLE_MEMALIGN=OFF \
  26. -DWITH_ADE=OFF \
  27. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  28. ..
  29. make -j8
  30. make install

      2. Android:在Ubuntu 16.04上交叉编译

      调整源码:

      (1).调整3rdparty/libpng/pngpriv.h:在第137行增加:#define PNG_ARM_NEON_OPT 0,强制关闭NEON,否则在编译imgcodecs模块时会报undefined symbol的error,如ld: error: undefined symbol: png_riffle_palette_neon,这是因为我们前面在编译OpenCV时设置关闭了加速。

      (2).调整cmake/OpenCVUtils.cmake:增加函数modify_android_link_deps,并在函数ocv_target_link_libraries内增加对modify_android_link_deps的调用:否则在编译android时会查找rt库,错误信息如下:ld: error: unable to find library -lrt,但是在android上是不需要rt库,这里通过判断是不是编译的android的库,若是,则将link_deps中的rt替换为log,log库在android是需要的

  1. function(modify_android_link_deps link_deps)
  2. set(new_link_deps "")
  3. foreach(dep ${${link_deps}})
  4. if("${dep}" STREQUAL "rt")
  5. list(APPEND new_link_deps "log")
  6. else()
  7. list(APPEND new_link_deps "${dep}")
  8. endif()
  9. endforeach()
  10. set(${link_deps} ${new_link_deps} PARENT_SCOPE)
  11. endfunction()
  12. function(ocv_target_link_libraries target)
  13. set(LINK_DEPS ${ARGN})
  14. string(FIND ${PLATFORM_ARCH_TOOLCHAIN} "android" compare_var)
  15. if(${compare_var} GREATER_EQUAL 0)
  16. modify_android_link_deps(LINK_DEPS)
  17. endif()
  18. _ocv_fix_target(target)
  19. set(LINK_MODE "PRIVATE")
  20. # ......
  21. endfunction()

      (3).使用gcc而不是clang编译android库时,会报一些error,如error: 'cbrt' is not a member of 'std'类似错误,这是因为android gcc缺少一些std函数的实现,这里新增加一个modules/core/include/opencv2/core/extend_std_funcs.hpp文件,仅用于实现满足在android gcc下缺少的这些std函数,并将此函数在当前目录的base.hpp文件中include它

  1. #ifndef EXTEND_STD_FUNCS_HPP
  2. #define EXTEND_STD_FUNCS_HPP
  3. #if (defined(__ANDROID__) && defined(__GNUC__) && !defined(__clang__)) || defined(LINUX_ARMV7_HIMIX100)
  4. #include <stdlib.h>
  5. #include <cmath>
  6. #include <string>
  7. #include <sstream>
  8. namespace std {
  9. #if defined(__arm__) || defined(__i386__)
  10. inline int sign(double val)
  11. {
  12. return (double(0) < val) - (val < double(0));
  13. }
  14. inline double cbrt(double x)
  15. {
  16. if (sign(x) == 0) return 0.;
  17. return sign(x) > 0 ? std::pow(x, double(1/3.)) : -std::pow(std::fabs(x), double(1/3.));
  18. }
  19. inline double copysign(double mag, double sgn)
  20. {
  21. if (sign(sgn) == 0) return mag;
  22. return sign(sgn) > 0 ? std::fabs(mag) : -std::fabs(mag);
  23. }
  24. #endif
  25. #if (defined(__ANDROID__) && defined(__GNUC__) && !defined(__clang__))
  26. inline unsigned long long stoull(const std::string& str, std::size_t* pos = nullptr, int base = 10)
  27. {
  28. (void)(pos);
  29. char* pEnd = nullptr;
  30. return strtoull(str.data(), &pEnd, base);
  31. }
  32. inline std::string to_string(int value)
  33. {
  34. std::ostringstream os;
  35. os << value ;
  36. return os.str();
  37. }
  38. #endif
  39. } // namespace std
  40. #endif
  41. #endif // EXTEND_STD_FUNCS_HPP

      (4).编译android x86 gcc时,调整主CMakeLists.txt文件,对CMAKE_C_FLAGS和CMAKE_CXX_FLAGS追加-m32选项。

      (5).编译android x86 clang时,调整主CMakeLists.txt文件,对CMAKE_C_FLAGS和CMAKE_CXX_FLAGS追加-m32选项。

      编译:

      (1).编译android-armv7-clang系列,如andorid-armv7-clang-r23b,则编译命令如下:

  1. elif [ $1 == "android-armv7-clang-r23b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="armeabi-v7a" \
  7. -DANDROID_ARM_MODE="arm" \
  8. -DANDROID_PLATFORM="android-21" \
  9. -DANDROID_TOOLCHAIN="clang" \
  10. -DCPU_BASELINE_DISABLE=ON \
  11. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang \
  12. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang++ \
  13. -DBUILD_SHARED_LIBS=ON \
  14. -DWITH_GTK=OFF \
  15. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  16. ..
  17. make -j8
  18. make install
  19. echo "#### build static libraries ..."
  20. rm -rf *
  21. cmake \
  22. ${common_options} \
  23. -DANDROID_ABI="armeabi-v7a" \
  24. -DANDROID_ARM_MODE="arm" \
  25. -DANDROID_PLATFORM="android-21" \
  26. -DANDROID_TOOLCHAIN="clang" \
  27. -DCPU_BASELINE_DISABLE=ON \
  28. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang \
  29. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang++ \
  30. -DBUILD_SHARED_LIBS=OFF \
  31. -DWITH_GTK=OFF \
  32. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  33. ..
  34. make -j8
  35. make install

      (2).编译android-aarch64-clang系列,如android-aarch64-clang-r23b,则编译命令如下:

  1. elif [ $1 == "android-aarch64-clang-r23b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="arm64-v8a" \
  7. -DANDROID_ARM_MODE="arm64" \
  8. -DANDROID_PLATFORM="android-21" \
  9. -DANDROID_TOOLCHAIN="clang" \
  10. -DCPU_BASELINE_DISABLE=ON \
  11. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang \
  12. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang++ \
  13. -DBUILD_SHARED_LIBS=ON \
  14. -DWITH_GTK=OFF \
  15. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  16. ..
  17. make -j8
  18. make install
  19. echo "#### build static libraries ..."
  20. rm -rf *
  21. cmake \
  22. ${common_options} \
  23. -DANDROID_ABI="arm64-v8a" \
  24. -DANDROID_ARM_MODE="arm64" \
  25. -DANDROID_PLATFORM="android-21" \
  26. -DANDROID_TOOLCHAIN="clang" \
  27. -DCPU_BASELINE_DISABLE=ON \
  28. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang \
  29. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang++ \
  30. -DBUILD_SHARED_LIBS=OFF \
  31. -DWITH_GTK=OFF \
  32. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  33. ..
  34. make -j8
  35. make install

      (3).编译android-x86-clang系列,如android-x86-clang-r23b,则编译命令如下:

  1. elif [ $1 == "android-x86-clang-r23b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="x86" \
  7. -DANDROID_PLATFORM="android-21" \
  8. -DANDROID_TOOLCHAIN="clang" \
  9. -DCPU_BASELINE_DISABLE=ON \
  10. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/i686-linux-android21-clang \
  11. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/i686-linux-android21-clang++ \
  12. -DBUILD_SHARED_LIBS=ON \
  13. -DWITH_GTK=OFF \
  14. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  15. ..
  16. make -j8
  17. make install
  18. echo "#### build static libraries ..."
  19. rm -rf *
  20. cmake \
  21. ${common_options} \
  22. -DANDROID_ABI="x86" \
  23. -DANDROID_PLATFORM="android-21" \
  24. -DANDROID_TOOLCHAIN="clang" \
  25. -DCPU_BASELINE_DISABLE=ON \
  26. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/i686-linux-android21-clang \
  27. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/i686-linux-android21-clang++ \
  28. -DBUILD_SHARED_LIBS=OFF \
  29. -DWITH_GTK=OFF \
  30. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  31. ..
  32. make -j8
  33. make install

      (4).编译android-x86_64-clang系列,如android-x86_64-clang-r23b,则编译命令如下:

  1. elif [ $1 == "android-x86_64-clang-r23b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="x86_64" \
  7. -DANDROID_PLATFORM="android-21" \
  8. -DANDROID_TOOLCHAIN="clang" \
  9. -DCPU_BASELINE_DISABLE=ON \
  10. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/x86_64-linux-android21-clang \
  11. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/x86_64-linux-android21-clang++ \
  12. -DBUILD_SHARED_LIBS=ON \
  13. -DWITH_GTK=OFF \
  14. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  15. ..
  16. make -j8
  17. make install
  18. echo "#### build static libraries ..."
  19. rm -rf *
  20. cmake \
  21. ${common_options} \
  22. -DANDROID_ABI="x86_64" \
  23. -DANDROID_PLATFORM="android-21" \
  24. -DANDROID_TOOLCHAIN="clang" \
  25. -DCPU_BASELINE_DISABLE=ON \
  26. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/x86_64-linux-android21-clang \
  27. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r23b/clang/prebuilt/linux-x86_64/bin/x86_64-linux-android21-clang++ \
  28. -DBUILD_SHARED_LIBS=OFF \
  29. -DWITH_GTK=OFF \
  30. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  31. ..
  32. make -j8
  33. make install

      (5).编译android-armv7-gcc-r14b,编译命令如下:

  1. elif [ $1 == "android-armv7-gcc-r14b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="armeabi-v7a" \
  7. -DANDROID_ARM_MODE="arm" \
  8. -DANDROID_PLATFORM="android-21" \
  9. -DANDROID_TOOLCHAIN="gcc" \
  10. -DCPU_BASELINE_DISABLE=ON \
  11. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-armv7/bin/arm-linux-androideabi-gcc \
  12. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-armv7/bin/arm-linux-androideabi-g++ \
  13. -DBUILD_SHARED_LIBS=ON \
  14. -DWITH_GTK=OFF \
  15. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  16. ..
  17. make -j8
  18. make install
  19. echo "#### build static libraries ..."
  20. rm -rf *
  21. cmake \
  22. ${common_options} \
  23. -DANDROID_ABI="armeabi-v7a" \
  24. -DANDROID_ARM_MODE="arm" \
  25. -DANDROID_PLATFORM="android-21" \
  26. -DANDROID_TOOLCHAIN="gcc" \
  27. -DCPU_BASELINE_DISABLE=ON \
  28. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-armv7/bin/arm-linux-androideabi-gcc \
  29. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-armv7/bin/arm-linux-androideabi-g++ \
  30. -DBUILD_SHARED_LIBS=OFF \
  31. -DWITH_GTK=OFF \
  32. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  33. ..
  34. make -j8
  35. make install

      (6).编译android-aarch64-gcc-r14b,编译命令如下:

  1. elif [ $1 == "android-aarch64-gcc-r14b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="arm64-v8a" \
  7. -DANDROID_ARM_MODE="arm64" \
  8. -DANDROID_PLATFORM="android-21" \
  9. -DANDROID_TOOLCHAIN="gcc" \
  10. -DCPU_BASELINE_DISABLE=ON \
  11. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-aarch64/bin/aarch64-linux-android-gcc \
  12. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-aarch64/bin/aarch64-linux-android-g++ \
  13. -DBUILD_SHARED_LIBS=ON \
  14. -DWITH_GTK=OFF \
  15. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  16. ..
  17. make -j8
  18. make install
  19. echo "#### build static libraries ..."
  20. rm -rf *
  21. cmake \
  22. ${common_options} \
  23. -DANDROID_ABI="arm64-v8a" \
  24. -DANDROID_ARM_MODE="arm64" \
  25. -DANDROID_PLATFORM="android-21" \
  26. -DANDROID_TOOLCHAIN="gcc" \
  27. -DCPU_BASELINE_DISABLE=ON \
  28. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-aarch64/bin/aarch64-linux-android-gcc \
  29. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-aarch64/bin/aarch64-linux-android-g++ \
  30. -DBUILD_SHARED_LIBS=OFF \
  31. -DWITH_GTK=OFF \
  32. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  33. ..
  34. make -j8
  35. make install

      (7).编译android-x86-gcc-r14b,编译命令如下:

  1. elif [ $1 == "android-x86-gcc-r14b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="x86" \
  7. -DANDROID_PLATFORM="android-21" \
  8. -DANDROID_TOOLCHAIN="gcc" \
  9. -DCPU_BASELINE_DISABLE=ON \
  10. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86/bin/i686-linux-android-gcc \
  11. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86/bin/i686-linux-android-g++ \
  12. -DBUILD_SHARED_LIBS=ON \
  13. -DWITH_GTK=OFF \
  14. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  15. ..
  16. make -j8
  17. make install
  18. echo "#### build static libraries ..."
  19. rm -rf *
  20. cmake \
  21. ${common_options} \
  22. -DANDROID_ABI="x86" \
  23. -DANDROID_PLATFORM="android-21" \
  24. -DANDROID_TOOLCHAIN="gcc" \
  25. -DCPU_BASELINE_DISABLE=ON \
  26. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86/bin/i686-linux-android-gcc \
  27. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86/bin/i686-linux-android-g++ \
  28. -DBUILD_SHARED_LIBS=OFF \
  29. -DWITH_GTK=OFF \
  30. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  31. ..
  32. make -j8
  33. make install

      (8).编译android-x86_64-gcc-r14b,编译命令如下:

  1. elif [ $1 == "android-x86_64-gcc-r14b" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DANDROID_ABI="x86_64" \
  7. -DANDROID_PLATFORM="android-21" \
  8. -DANDROID_TOOLCHAIN="gcc" \
  9. -DCPU_BASELINE_DISABLE=ON \
  10. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86_64/bin/x86_64-linux-android-gcc \
  11. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86_64/bin/x86_64-linux-android-g++ \
  12. -DBUILD_SHARED_LIBS=ON \
  13. -DWITH_GTK=OFF \
  14. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  15. ..
  16. make -j8
  17. make install
  18. echo "#### build static libraries ..."
  19. rm -rf *
  20. cmake \
  21. ${common_options} \
  22. -DANDROID_ABI="x86_64" \
  23. -DANDROID_PLATFORM="android-21" \
  24. -DANDROID_TOOLCHAIN="gcc" \
  25. -DCPU_BASELINE_DISABLE=ON \
  26. -DCMAKE_C_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86_64/bin/x86_64-linux-android-gcc \
  27. -DCMAKE_CXX_COMPILER=${toolchains_path}/android-ndk-r14b/gcc/android-21/android-x86_64/bin/x86_64-linux-android-g++ \
  28. -DBUILD_SHARED_LIBS=OFF \
  29. -DWITH_GTK=OFF \
  30. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  31. ..
  32. make -j8
  33. make install

      3. Wasm:在Ubuntu 16.04上交叉编译

      通用项定义:

  1. wasm_common_options="--cmake_option=-DCMAKE_BUILD_TYPE=RELEASE \
  2. --cmake_option=-DCMAKE_CXX_FLAGS=-fPIC \
  3. --cmake_option=-DCMAKE_C_FLAGS=-fPIC \
  4. --cmake_option=-DPLATFORM_ARCH_TOOLCHAIN=$1 \
  5. --cmake_option=-DBUILD_SHARED_LIBS=OFF \
  6. --cmake_option=-DBUILD_PROTOBUF=OFF \
  7. --cmake_option=-DBUILD_ZLIB=ON \
  8. --cmake_option=-DBUILD_opencv_apps=OFF \
  9. --cmake_option=-DBUILD_opencv_gapi=OFF \
  10. --cmake_option=-DBUILD_opencv_highgui=ON \
  11. --cmake_option=-DBUILD_opencv_imgcodecs=ON \
  12. --cmake_option=-DBUILD_opencv_photo=ON \
  13. --cmake_option=-DBUILD_opencv_stitching=ON \
  14. --cmake_option=-DBUILD_opencv_dnn=OFF \
  15. --cmake_option=-DBUILD_opencv_objdetect=OFF \
  16. --cmake_option=-DBUILD_opencv_ts=OFF \
  17. --cmake_option=-DBUILD_opencv_ml=ON \
  18. --cmake_option=-DBUILD_opencv_videoio=ON \
  19. --cmake_option=-DOPENCV_FORCE_3RDPARTY_BUILD=ON \
  20. --cmake_option=-DBUILD_JASPER=ON \
  21. --cmake_option=-DBUILD_JPEG=ON \
  22. --cmake_option=-DBUILD_PNG=ON \
  23. --cmake_option=-DBUILD_TIFF=ON \
  24. --cmake_option=-DBUILD_WEBP=ON"

      依赖项:本机需安装emcmake;本机需安装python3,版本要求>=3.2

      调整源码:

      (1).编译x86时,调整主CMakeLists.txt文件,对CMAKE_C_FLAGS和CMAKE_CXX_FLAGS追加-m32选项。

      编译:

      (1).编译wasm-x86,编译命令如下:

  1. elif [ $1 == "wasm-x86" ]; then
  2. echo "#### build static libraries ..."
  3. rm -rf *
  4. TARGET_COMPILER_VERSION=1.39.20
  5. emcmake \
  6. python3 ../platforms/js/build_js.py . \
  7. --emscripten_dir=${toolchains_path}/emsdk-${TARGET_COMPILER_VERSION}/upstream/emscripten/ \
  8. ${wasm_common_options} \
  9. --cmake_option="-DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static}"
  10. emmake make install

      (2).编译wasm-x86_64,编译命令如下:

  1. elif [ $1 == "wasm-x86_64" ]; then
  2. echo "#### build static libraries ..."
  3. rm -rf *
  4. TARGET_COMPILER_VERSION=1.39.20
  5. emcmake \
  6. python3 ../platforms/js/build_js.py . \
  7. --emscripten_dir=${toolchains_path}/emsdk-${TARGET_COMPILER_VERSION}/upstream/emscripten/ \
  8. ${wasm_common_options} \
  9. --cmake_option="-DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static}"
  10. emmake make install

      4. Windows 10:

      cmake/OpenCVCRTLinkage.cmake中,默认动态库是MD,静态库是MT,这里作了调整,无论是动态库还是静态库都可以指定是MD还是MT。为了让windows 10编译的库可以在windows xp上运行,这里指定了v140_xp选项。

      基于windows的通用项定义如下:

  1. msvc_mt_common_options="-DCMAKE_CXX_FLAGS=/DWIN32 /D_WINDOWS /W3 /GR /EHsc \
  2. -DCMAKE_CXX_FLAGS_DEBUG=/MTd /Zi /Ob0 /Od /RTC1 \
  3. -DCMAKE_CXX_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG \
  4. -DCMAKE_CXX_FLAGS_MINSIZEREL=/MT /O1 /Ob1 /DNDEBUG \
  5. -DCMAKE_CXX_FLAGS_RELWITHDEBINFO=/MT /Zi /O2 /Ob1 /DNDEBUG \
  6. -DCMAKE_C_FLAGS=/DWIN32 /D_WINDOWS /W3 \
  7. -DCMAKE_C_FLAGS_DEBUG=/MTd /Zi /Ob0 /Od /RTC1 \
  8. -DCMAKE_C_FLAGS_RELEASE=/MT /O2 /Ob2 /DNDEBUG \
  9. -DCMAKE_C_FLAGS_MINSIZEREL=/MT /O1 /Ob1 /DNDEBUG \
  10. -DCMAKE_C_FLAGS_RELWITHDEBINFO=/MT /Zi /O2 /Ob1 /DNDEBUG \
  11. -DWITH_ADE=OFF \
  12. -DWITH_MSMF=OFF \
  13. -DBUILD_opencv_dnn=OFF"
  14. msvc_md_common_options="-DCMAKE_CXX_FLAGS=/DWIN32 /D_WINDOWS /W3 /GR /EHsc \
  15. -DCMAKE_CXX_FLAGS_DEBUG=/MDd /Zi /Ob0 /Od /RTC1 \
  16. -DCMAKE_CXX_FLAGS_RELEASE=/MD /O2 /Ob2 /DNDEBUG \
  17. -DCMAKE_CXX_FLAGS_MINSIZEREL=/MD /O1 /Ob1 /DNDEBUG \
  18. -DCMAKE_CXX_FLAGS_RELWITHDEBINFO=/MD /Zi /O2 /Ob1 /DNDEBUG \
  19. -DCMAKE_C_FLAGS=/DWIN32 /D_WINDOWS /W3 \
  20. -DCMAKE_C_FLAGS_DEBUG=/MDd /Zi /Ob0 /Od /RTC1 \
  21. -DCMAKE_C_FLAGS_RELEASE=/MD /O2 /Ob2 /DNDEBUG \
  22. -DCMAKE_C_FLAGS_MINSIZEREL=/MD /O1 /Ob1 /DNDEBUG \
  23. -DCMAKE_C_FLAGS_RELWITHDEBINFO=/MD /Zi /O2 /Ob1 /DNDEBUG \
  24. -DWITH_ADE=OFF \
  25. -DWITH_MSMF=OFF \
  26. -DBUILD_opencv_dnn=OFF"

      调整源码:

      (1).关闭python的支持,否则会报error:ImportError: No module named numpy.distutils,调整主CMakeLists.txt,将

  1. # --- Python Support ---
  2. if(NOT IOS)
  3. include(cmake/OpenCVDetectPython.cmake)
  4. endif()

      调整为:

  1. # --- Python Support ---
  2. if((NOT IOS) AND (NOT CMAKE_GENERATOR MATCHES "Visual Studio"))
  3. include(cmake/OpenCVDetectPython.cmake)
  4. endif()

      (2).由于增加了v140_xp选项,需关闭FLS的支持,否则会报error:fatal error C1083: Cannot open include file: 'fibersapi.h': No such file or directory,调整主CMakeLists.txt,设置变量ST_COMPATIBLE_XP,如下:

  1. if(${PLATFORM_ARCH_TOOLCHAIN} STREQUAL "windows-x86-vs2015" OR ${PLATFORM_ARCH_TOOLCHAIN} STREQUAL "windows-x86_64-vs2015")
  2. add_definitions(-DST_COMPATIBLE_XP)
  3. endif()

      调整modules/core/src/system.cpp,强制关闭FLS,如下

  1. #if (_WIN32_WINNT >= 0x0602)
  2. #include <synchapi.h>
  3. #endif
  4. #ifdef ST_COMPATIBLE_XP
  5. #define CV_DISABLE_FLS // force close FLS when use v140_xp
  6. #endif
  7. #if ((_WIN32_WINNT >= 0x0600) && !defined(CV_DISABLE_FLS)) || defined(CV_FORCE_FLS)
  8. #include <fibersapi.h>
  9. #define CV_USE_FLS
  10. #endif

      (3).由于动态库和静态库都需要同时支持MD和MT,这里在build.sh中定义ST_FORCE_MSVC_USE_MT和ST_FORCE_MSVC_USE_MD用来指定是编译MD模式还是MT模式,调整cmake/OpenCVRTLinkage.cmake,将原来的if(NOT BUILD_SHARED_LIBS AND BUILD_WITH_STATIC_CRT)调整为

if((NOT ST_FORCE_MSVC_USE_MD) AND ((NOT BUILD_SHARED_LIBS AND BUILD_WITH_STATIC_CRT) OR ST_FORCE_MSVC_USE_MT))

      编译:

      (1).编译windows-x86_64-vs系列,如windows-x86_64-vs2015,则编译命令如下:

  1. elif [ $1 == "windows-x86_64-vs2015" ]; then
  2. echo "#### build mt dynamic libraries debug ..."
  3. rm -rf *
  4. cmake \
  5. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  6. ${common_options} \
  7. ${msvc_mt_common_options} \
  8. -DCMAKE_BUILD_TYPE=Debug \
  9. -DCMAKE_CONFIGURATION_TYPES=Debug \
  10. -DST_FORCE_MSVC_USE_MT=1 \
  11. -DBUILD_SHARED_LIBS=ON \
  12. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  13. ..
  14. cmake --build . --target install --config debug
  15. echo "#### build mt dynamic libraries release ..."
  16. rm -rf *
  17. cmake \
  18. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  19. ${common_options} \
  20. ${msvc_mt_common_options} \
  21. -DCMAKE_BUILD_TYPE=Release \
  22. -DCMAKE_CONFIGURATION_TYPES=Release \
  23. -DST_FORCE_MSVC_USE_MT=1 \
  24. -DBUILD_SHARED_LIBS=ON \
  25. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  26. ..
  27. cmake --build . --target install --config release
  28. echo "#### build mt static libraries debug ..."
  29. rm -rf *
  30. cmake \
  31. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  32. ${common_options} \
  33. ${msvc_mt_common_options} \
  34. -DCMAKE_BUILD_TYPE=Debug \
  35. -DCMAKE_CONFIGURATION_TYPES=Debug \
  36. -DST_FORCE_MSVC_USE_MT=1 \
  37. -DBUILD_SHARED_LIBS=OFF \
  38. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  39. ..
  40. cmake --build . --target install --config debug
  41. echo "#### build mt static libraries release ..."
  42. rm -rf *
  43. cmake \
  44. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  45. ${common_options} \
  46. ${msvc_mt_common_options} \
  47. -DCMAKE_BUILD_TYPE=Release \
  48. -DCMAKE_CONFIGURATION_TYPES=Release \
  49. -DST_FORCE_MSVC_USE_MT=1 \
  50. -DBUILD_SHARED_LIBS=OFF \
  51. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  52. ..
  53. cmake --build . --target install --config release
  54. echo "#### build md dynamic libraries debug ..."
  55. rm -rf *
  56. cmake \
  57. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  58. ${common_options} \
  59. ${msvc_md_common_options} \
  60. -DCMAKE_BUILD_TYPE=Debug \
  61. -DCMAKE_CONFIGURATION_TYPES=Debug \
  62. -DST_FORCE_MSVC_USE_MD=1 \
  63. -DBUILD_SHARED_LIBS=ON \
  64. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  65. ..
  66. cmake --build . --target install --config debug
  67. echo "#### build md dynamic libraries release ..."
  68. rm -rf *
  69. cmake \
  70. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  71. ${common_options} \
  72. ${msvc_md_common_options} \
  73. -DCMAKE_BUILD_TYPE=Release \
  74. -DCMAKE_CONFIGURATION_TYPES=Release \
  75. -DST_FORCE_MSVC_USE_MD=1 \
  76. -DBUILD_SHARED_LIBS=ON \
  77. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  78. ..
  79. cmake --build . --target install --config release
  80. echo "#### build md static libraries debug ..."
  81. rm -rf *
  82. cmake \
  83. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  84. ${common_options} \
  85. ${msvc_md_common_options} \
  86. -DCMAKE_BUILD_TYPE=Debug \
  87. -DCMAKE_CONFIGURATION_TYPES=Debug \
  88. -DST_FORCE_MSVC_USE_MD=1 \
  89. -DBUILD_SHARED_LIBS=OFF \
  90. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  91. ..
  92. cmake --build . --target install --config debug
  93. echo "#### build md static libraries release ..."
  94. rm -rf *
  95. cmake \
  96. -G"Visual Studio 14 2015" -A x64 -T v140_xp \
  97. ${common_options} \
  98. ${msvc_md_common_options} \
  99. -DCMAKE_BUILD_TYPE=Release \
  100. -DCMAKE_CONFIGURATION_TYPES=Release \
  101. -DST_FORCE_MSVC_USE_MD=1 \
  102. -DBUILD_SHARED_LIBS=OFF \
  103. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  104. ..
  105. cmake --build . --target install --config release

      (2).编译windows-x86-vs系列,如windows-x86_64-vs2015,则编译命令如下:

  1. elif [ $1 == "windows-x86-vs2015" ]; then
  2. echo "#### build mt dynamic libraries debug ..."
  3. rm -rf *
  4. cmake \
  5. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  6. ${common_options} \
  7. ${msvc_mt_common_options} \
  8. -DCMAKE_BUILD_TYPE=Debug \
  9. -DCMAKE_CONFIGURATION_TYPES=Debug \
  10. -DST_FORCE_MSVC_USE_MT=1 \
  11. -DBUILD_SHARED_LIBS=ON \
  12. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  13. ..
  14. cmake --build . --target install --config debug
  15. echo "#### build mt dynamic libraries release ..."
  16. rm -rf *
  17. cmake \
  18. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  19. ${common_options} \
  20. ${msvc_mt_common_options} \
  21. -DCMAKE_BUILD_TYPE=Release \
  22. -DCMAKE_CONFIGURATION_TYPES=Release \
  23. -DST_FORCE_MSVC_USE_MT=1 \
  24. -DBUILD_SHARED_LIBS=ON \
  25. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  26. ..
  27. cmake --build . --target install --config release
  28. echo "#### build mt static libraries debug ..."
  29. rm -rf *
  30. cmake \
  31. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  32. ${common_options} \
  33. ${msvc_mt_common_options} \
  34. -DCMAKE_BUILD_TYPE=Debug \
  35. -DCMAKE_CONFIGURATION_TYPES=Debug \
  36. -DST_FORCE_MSVC_USE_MT=1 \
  37. -DBUILD_SHARED_LIBS=OFF \
  38. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  39. ..
  40. cmake --build . --target install --config debug
  41. echo "#### build mt static libraries release ..."
  42. rm -rf *
  43. cmake \
  44. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  45. ${common_options} \
  46. ${msvc_mt_common_options} \
  47. -DCMAKE_BUILD_TYPE=Release \
  48. -DCMAKE_CONFIGURATION_TYPES=Release \
  49. -DST_FORCE_MSVC_USE_MT=1 \
  50. -DBUILD_SHARED_LIBS=OFF \
  51. -DCMAKE_INSTALL_PREFIX=../install/$1/mt \
  52. ..
  53. cmake --build . --target install --config release
  54. echo "#### build md dynamic libraries debug ..."
  55. rm -rf *
  56. cmake \
  57. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  58. ${common_options} \
  59. ${msvc_md_common_options} \
  60. -DCMAKE_BUILD_TYPE=Debug \
  61. -DCMAKE_CONFIGURATION_TYPES=Debug \
  62. -DST_FORCE_MSVC_USE_MD=1 \
  63. -DBUILD_SHARED_LIBS=ON \
  64. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  65. ..
  66. cmake --build . --target install --config debug
  67. echo "#### build md dynamic libraries release ..."
  68. rm -rf *
  69. cmake \
  70. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  71. ${common_options} \
  72. ${msvc_md_common_options} \
  73. -DCMAKE_BUILD_TYPE=Release \
  74. -DCMAKE_CONFIGURATION_TYPES=Release \
  75. -DST_FORCE_MSVC_USE_MD=1 \
  76. -DBUILD_SHARED_LIBS=ON \
  77. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  78. ..
  79. cmake --build . --target install --config release
  80. echo "#### build md static libraries debug ..."
  81. rm -rf *
  82. cmake \
  83. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  84. ${common_options} \
  85. ${msvc_md_common_options} \
  86. -DCMAKE_BUILD_TYPE=Debug \
  87. -DCMAKE_CONFIGURATION_TYPES=Debug \
  88. -DST_FORCE_MSVC_USE_MD=1 \
  89. -DBUILD_SHARED_LIBS=OFF \
  90. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  91. ..
  92. cmake --build . --target install --config debug
  93. echo "#### build md static libraries release ..."
  94. rm -rf *
  95. cmake \
  96. -G"Visual Studio 14 2015" -A Win32 -T v140_xp \
  97. ${common_options} \
  98. ${msvc_md_common_options} \
  99. -DCMAKE_BUILD_TYPE=Release \
  100. -DCMAKE_CONFIGURATION_TYPES=Release \
  101. -DST_FORCE_MSVC_USE_MD=1 \
  102. -DBUILD_SHARED_LIBS=OFF \
  103. -DCMAKE_INSTALL_PREFIX=../install/$1/md \
  104. ..
  105. cmake --build . --target install --config release

      5. MacOS:

      调整源码:

      (1).调整主CMakeLists.txt,强制使HAVE_PTHREAD不生效,否则报error:CMake Error at CMakeLists.txt:Thread execution might be in use in some component,在build.sh中定义ST_FORCE_XCODE_CLOSE_GCD,然后将CMakeLists.txt中的

  1. else())
  2. set(HAVE_PTHREAD 1)
  3. endif()

      调整为:

  1. elseif(NOT ST_FORCE_XCODE_CLOSE_GCD)
  2. set(HAVE_PTHREAD 1)
  3. endif()

      (2).调整主CMakeLists.txt,强制使list(REMOVE_ITEM OPENCV_LINKER_LIBS pthread)不生效,否则报error:CMake Error at CMakeLists.txt: (list):list sub-command REMOVE_ITEM requires list to be present,将CMakeLists.txt中的

  1. if (OPENCV_DISABLE_THREAD_SUPPORT)
  2. list(REMOVE_ITEM OPENCV_LINKER_LIBS pthread)
  3. endif()

      调整为:

  1. if (OPENCV_DISABLE_THREAD_SUPPORT AND NOT ST_FORCE_XCODE_CLOSE_GCD)
  2. list(REMOVE_ITEM OPENCV_LINKER_LIBS pthread)
  3. endif()

      (3).调整cmake/OpenCVFindFrameworks.cmake,强制使HAVE_GCD不生效,否则报error:CMake Error at CMakeLists.txt: (message): Not all parallel frameworks have been disabled (using GCD),将OpenCVFindFrameworks.cmake中的

  1. if(APPLE AND NOT HAVE_TBB)
  2. set(HAVE_GCD 1)
  3. else()

      调整为:

  1. if(APPLE AND NOT HAVE_TBB AND NOT ST_FORCE_XCODE_CLOSE_GCD)
  2. set(HAVE_GCD 1)
  3. else()

      (4).编译osx x86的库时调整主CMakeLists.txt文件,对CMAKE_C_FLAGS和CMAKE_CXX_FLAGS追加-m32选项。

      编译:

      (1).编译osx-x86_64-xcode系列,如osx-x86_64-xcode12,则编译命令如下:

  1. elif [ $1 == "osx-x86_64-xcode12" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc \
  7. -DCMAKE_CXX_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DWITH_ADE=OFF \
  10. -DST_FORCE_XCODE_CLOSE_GCD=1 \
  11. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  12. ..
  13. make -j8
  14. make install
  15. echo "#### build static libraries ..."
  16. rm -rf *
  17. cmake \
  18. ${common_options} \
  19. -DCMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc \
  20. -DCMAKE_CXX_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ \
  21. -DBUILD_SHARED_LIBS=OFF \
  22. -DWITH_ADE=OFF \
  23. -DST_FORCE_XCODE_CLOSE_GCD=1 \
  24. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  25. ..
  26. make -j8
  27. make install

      (2).编译osx-x86-xcode系列,如osx-x86-xcode12,则编译命令如下:

  1. elif [ $1 == "osx-x86-xcode12" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc \
  7. -DCMAKE_CXX_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DWITH_ADE=OFF \
  10. -DST_FORCE_XCODE_CLOSE_GCD=1 \
  11. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  12. ..
  13. make -j8
  14. make install
  15. echo "#### build static libraries ..."
  16. rm -rf *
  17. cmake \
  18. ${common_options} \
  19. -DCMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc \
  20. -DCMAKE_CXX_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ \
  21. -DBUILD_SHARED_LIBS=OFF \
  22. -DWITH_ADE=OFF \
  23. -DST_FORCE_XCODE_CLOSE_GCD=1 \
  24. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  25. ..
  26. make -j8
  27. make install

      (3).编译osx-aarch64-xcode系列,如osx-aarch64-xcode12,则编译命令如下:Apple M1

  1. elif [ $1 == "osx-aarch64-xcode12" ]; then
  2. echo "#### build dynamic libraries ..."
  3. rm -rf *
  4. cmake \
  5. ${common_options} \
  6. -DCMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc \
  7. -DCMAKE_CXX_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ \
  8. -DBUILD_SHARED_LIBS=ON \
  9. -DWITH_ADE=OFF \
  10. -DST_FORCE_XCODE_CLOSE_GCD=1 \
  11. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_dynamic} \
  12. ..
  13. make -j8
  14. make install
  15. echo "#### build static libraries ..."
  16. rm -rf *
  17. cmake \
  18. ${common_options} \
  19. -DCMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc \
  20. -DCMAKE_CXX_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ \
  21. -DBUILD_SHARED_LIBS=OFF \
  22. -DWITH_ADE=OFF \
  23. -DST_FORCE_XCODE_CLOSE_GCD=1 \
  24. -DCMAKE_INSTALL_PREFIX=../install/$1/${dir_name_static} \
  25. ..
  26. make -j8
  27. make install

      6. iOS

      基于iOS的通用项定义如下:

  1. ios_common_options="--without world --without gapi --without dnn --without objdetect --without objc --without ts \
  2. --disable 1394 --disable EIGEN --disable CUDA --disable FFMPEG --disable IPP --disable ITT --disable LAPACK \
  3. --disable LIBREALSENSE --disable NVCUVID --disable OPENCL --disable OPENEXR --disable OPENGL --disable OPENMP \
  4. --disable OPENVX --disable PROTOBUF --disable QT --disable TBB --disable ADE --disable QUIRC"

      调整源码:主要调整platforms/ios/build_framework.py

      (1).调整platforms/ios/build_framework.py,在_build函数内的cmake_flags = []后添加一句,否则在主CMakeLists.txt里会报error:CMake Error at CMakeLists.txt: (if): Unknown arguments specified,因为PLATFORM_ARCH_TOOLCHAIN变量没有被定义

cmake_flags.append("-DPLATFORM_ARCH_TOOLCHAIN=ios")

      (2).调整platforms/ios/build_framework.py,对于--iphoneos_archs和--iphonesimulator_archs选项,如果不显示指定则会被赋予默认值,这里调整为--iphoneos_archs和--iphonesimulator_archs每次仅支持一种指定

  1. # iphoneos_archs = None
  2. # if args.iphoneos_archs:
  3. # iphoneos_archs = args.iphoneos_archs.split(',')
  4. # elif not args.build_only_specified_archs:
  5. # # Supply defaults
  6. # iphoneos_archs = ["armv7", "armv7s", "arm64"]
  7. # print('Using iPhoneOS ARCHS=' + str(iphoneos_archs))
  8. # iphonesimulator_archs = None
  9. # if args.iphonesimulator_archs:
  10. # iphonesimulator_archs = args.iphonesimulator_archs.split(',')
  11. # elif not args.build_only_specified_archs:
  12. # # Supply defaults
  13. # iphonesimulator_archs = ["i386", "x86_64"]
  14. # print('Using iPhoneSimulator ARCHS=' + str(iphonesimulator_archs))
  15. if args.iphoneos_archs is None and args.iphonesimulator_archs is None:
  16. print_error("--iphoneos_archs and --iphonesimulator_archs are undefined; nothing will be built.")
  17. sys.exit(1)
  18. if args.iphoneos_archs and args.iphonesimulator_archs:
  19. print_error("one of --iphoneos_archs and --iphonesimulator_archs must be specified.")
  20. sys.exit(1)
  21. iphoneos_archs = None
  22. if args.iphoneos_archs:
  23. iphoneos_archs = args.iphoneos_archs.split(',')
  24. print('Using iPhoneOS ARCHS=' + str(iphoneos_archs))
  25. iphonesimulator_archs = None
  26. if args.iphonesimulator_archs:
  27. iphonesimulator_archs = args.iphonesimulator_archs.split(',')
  28. print('Using iPhoneSimulator ARCHS=' + str(iphonesimulator_archs))

      编译:

      (1).编译ios_os-universal-xcode系列,如ios_os-universal-xcode12,真机,则编译命令如下:编译完后,会分别有基于armv7和arm64两套库,可以通过libtool将armv7和arm64整合成一套库

  1. elif [ $1 == "ios_os-universal-xcode12" ]; then
  2. echo "#### build static libraries ..."
  3. rm -rf *
  4. python ../platforms/ios/build_framework.py ../install/$1 --iphoneos_archs armv7,arm64 ${ios_common_options}
  5. echo "#### CreateUniversalBinary ..."
  6. path=../install/$1/
  7. path_armv7=build/build-armv7-iphoneos/install/lib/
  8. path_arm64=build/build-arm64-iphoneos/install/lib/
  9. prefix_3rdparty=3rdparty/lib
  10. prefix_modules=libopencv_
  11. suffix=.a
  12. name_3rd_libs=("libpng" "libwebp" "zlib" "libjpeg-turbo")
  13. name_module_libs=("photo" "videoio" "calib3d" "features2d" "core" "imgcodecs" "highgui" "video" "ml" "stitching" "flann" "imgproc")
  14. mkdir -p ${path}${dir_name_static}/lib/3rdparty
  15. mkdir -p ${path}${dir_name_static}/lib
  16. for val in ${name_3rd_libs[@]}; do
  17. name_armv7_lib="${path}${path_armv7}${prefix_3rdparty}${val}${suffix}"
  18. name_arm64_lib="${path}${path_arm64}${prefix_3rdparty}${val}${suffix}"
  19. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static ${name_armv7_lib} ${name_arm64_lib} -o ${path}${dir_name_static}/lib/${prefix_3rdparty}${val}${suffix}
  20. done
  21. for val in ${name_module_libs[@]}; do
  22. name_armv7_lib="${path}${path_armv7}${prefix_modules}${val}${suffix}"
  23. name_arm64_lib="${path}${path_arm64}${prefix_modules}${val}${suffix}"
  24. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static ${name_armv7_lib} ${name_arm64_lib} -o ${path}${dir_name_static}/lib/${prefix_modules}${val}${suffix}
  25. done
  26. cp -a ${path}${path_armv7}../include ${path}${dir_name_static}/
  27. cp -a ${path}${path_armv7}../share ${path}${dir_name_static}/
  28. cp -a ${path}${path_armv7}cmake ${path}${dir_name_static}/lib/

      (2).编译ios_sim-universal-xcode系列,如ios_sim-universal-xcode12,模拟器(simulator),则编译命令如下:编译完后,会分别有基于i386和x86_64两套库,可以通过libtool将i386和x86_64整合成一套库

  1. elif [ $1 == "ios_sim-universal-xcode12" ]; then
  2. echo "#### build static libraries ..."
  3. rm -rf *
  4. python ../platforms/ios/build_framework.py ../install/$1 --iphonesimulator_archs i386,x86_64 ${ios_common_options}
  5. echo "#### CreateUniversalBinary ..."
  6. path=../install/$1/
  7. path_i386=build/build-i386-iphonesimulator/install/lib/
  8. path_x86_64=build/build-x86_64-iphonesimulator/install/lib/
  9. prefix_3rdparty=3rdparty/lib
  10. prefix_modules=libopencv_
  11. suffix=.a
  12. name_3rd_libs=("libpng" "libwebp" "zlib" "libjpeg-turbo")
  13. name_module_libs=("photo" "videoio" "calib3d" "features2d" "core" "imgcodecs" "highgui" "video" "ml" "stitching" "flann" "imgproc")
  14. mkdir -p ${path}${dir_name_static}/lib/3rdparty
  15. mkdir -p ${path}${dir_name_static}/lib
  16. for val in ${name_3rd_libs[@]}; do
  17. name_i386_lib="${path}${path_i386}${prefix_3rdparty}${val}${suffix}"
  18. name_x86_64_lib="${path}${path_x86_64}${prefix_3rdparty}${val}${suffix}"
  19. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static ${name_i386_lib} ${name_x86_64_lib} -o ${path}${dir_name_static}/lib/${prefix_3rdparty}${val}${suffix}
  20. done
  21. for val in ${name_module_libs[@]}; do
  22. name_i386_lib="${path}${path_i386}${prefix_modules}${val}${suffix}"
  23. name_x86_64_lib="${path}${path_x86_64}${prefix_modules}${val}${suffix}"
  24. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool -static ${name_i386_lib} ${name_x86_64_lib} -o ${path}${dir_name_static}/lib/${prefix_modules}${val}${suffix}
  25. done
  26. cp -a ${path}${path_i386}../include ${path}${dir_name_static}/
  27. cp -a ${path}${path_i386}../share ${path}${dir_name_static}/
  28. cp -a ${path}${path_i386}cmake ${path}${dir_name_static}/lib/

      (3).编译ios_sim-aarch64-xcode系列,如ios_sim-aarch64-xcode12,模拟器(simulator),则编译命令如下:Apple M1

  1. elif [ $1 == "ios_sim-aarch64-xcode12" ]; then
  2. echo "#### build static libraries ..."
  3. rm -rf *
  4. python ../platforms/ios/build_framework.py ../install/$1 --iphonesimulator_archs arm64 ${ios_common_options}
  5. path=../install/$1/
  6. mkdir -p ${path}${dir_name_static}
  7. cp -a ${path}build/build-arm64-iphonesimulator/install/ ${path}${dir_name_static}/

      7. 项目中使用OpenCV

      (1).项目test中加入对OpenCV的支持,则CMakeLists.txt应包括类似语句:

  1. FIND_PACKAGE(OpenCV)
  2. INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
  3. MESSAGE("#### opencv include dirs: ${OpenCV_INCLUDE_DIRS}")
  4. MESSAGE("#### opencv libs: ${OpenCV_LIBS}")
  5. TARGET_LINK_LIBRARIES(test ${OpenCV_LIBS})

      (2).cmake时应使用OpenCV_DIR指定opencv中OpenCVConfig.cmake文件路径,如linux-x86_64-gcc5_4,这样主机上可以同时包含多个版本的opencv,通过OpenCV_DIR来指定使用哪个版本

cmake -DOpenCV_DIR=/usr/local/toolchains/opencv/4.6.0/linux-x86_64-gcc5_4/shared/lib/cmake/opencv4/ ..

      (3).因为opencv中编译选项较多,可通过调用cv::getBuildInformation()函数来查看编译opencv时开启了哪些选项.

      8. 因为编译的平台、架构较多,有时需要了解某个编译器(preprocessor)定义支持的宏,可通过执行如下命令获取,如/usr/bin/gcc和/usr/bin/g++

  1. echo | /usr/bin/gcc -dM -E -
  2. echo | /usr/bin/g++ -dM -E -x c++ -

      GitHubhttps://github.com/fengbingchun/OpenCV_Test

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号