使用 cmake 制作不支持 sse 的库(windows 版本)

Posted

技术标签:

【中文标题】使用 cmake 制作不支持 sse 的库(windows 版本)【英文标题】:using cmake to make a library without sse support (windows version) 【发布时间】:2019-09-19 09:24:19 【问题描述】:

我想使用 CMake for Visual Studio 2019 设置 OpenCV。在调试我的程序时完成所有这些过程后,我收到此错误:

yaabolfazl.exe 中 0x00007FFCF1FF2382 (opencv_world412d.dll) 处的未处理异常:0xC000001D:非法指令。发生了。

我认为这是由于我的处理器不支持 AVX(Windows 8.1,core i5 M520 2.4GHz)造成的。 (我对吗?) 我发现我可以使用 CPU_DISPATCHCPU_BASELINE 选项在不支持 AVX 的情况下使用 CMake 制作新版本的 OpenCV,但是当我更改它们的值时,我得到了这个错误:

配置过程出错,项目文件可能无效。

那么如何在不支持 AVX 的情况下使用 CMake 构建 OpenCV(例如,仅使用 SSE 支持)?

我的 CMake 配置

CPU指令集:

【问题讨论】:

回答您的嵌入式问题:不,您的CPU does not have AVX。 我们需要查看更详细的错误信息。 我假设你已经下载了 OpenCV 源码包;转到“\cmake\”文件夹并打开“OpenCVCompilerOptimizations.cmake”。搜索这两行:“ocv_optimization_process_obsolete_option(ENABLE_AVX AVX ON) ocv_optimization_process_obsolete_option(ENABLE_AVX2 AVX2 ON)”如果设置为OFF,有帮助吗? 您好,感谢您的重播。我加了一张截图。你还需要什么? 您也可以在使用 cmake 制作 OpenCV 时尝试设置 -DENABLE_AVX=OFF 和 -DENABLE_AVX2=OFF。我不使用 CMake GUI,但在命令行中它看起来像这样: cmake -DENABLE_AVX=OFF -DENABLE_AVX2=OFF -G "Visual Studio 16 2019" ..(假设你的 PATH 中有 cmake 并执行此命令从 opencv 文件夹中创建的构建文件夹) 【参考方案1】:

问题很可能是您的 CPU 不支持 OpenCV 使用的某些 CPU 功能(如 AVX)。我不确定在构建 OpenCV 时禁用这些功能的确切 CMake 标志是什么,但是还有另一种方法可以解决这个问题(蛮力方法:D)。从this link 下载 OpenCV 源码并解压(当前最新版本为 4.1.1)。 转到 opencv-4.1.1\cmake 文件夹并在文本编辑器中打开 OpenCVCompilerOptimizations.cmake。它包含针对不同 CPU 架构的优化。问题中附加的屏幕截图显示了您的 CPU 支持的指令。在 OpenCVCompilerOptimizations.cmake 文件中,只需删除(或注释掉)您的 CPU 不支持的每个指令选项。这是我的版本(我只包括了修改的文件的前半部分。将其与原始版本进行比较以查看差异):

# x86/x86-64 arch:
# SSE / SSE2 (always available on 64-bit CPUs)
# SSE3 / SSSE3
# SSE4_1 / SSE4_2 / POPCNT
# AVX / AVX2 / AVX_512F
# FMA3
#
# CPU features groups:
# AVX512_COMMON (Common instructions AVX-512F/CD for all CPUs that support AVX-512)
# AVX512_KNL (Knights Landing with AVX-512F/CD/ER/PF)
# AVX512_KNM (Knights Mill with AVX-512F/CD/ER/PF/4FMAPS/4VNNIW/VPOPCNTDQ)
# AVX512_SKX (Skylake-X with AVX-512F/CD/BW/DQ/VL)
# AVX512_CNL (Cannon Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI)
# AVX512_CEL (Cascade Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI/VNNI)
# AVX512_ICL (Ice Lake with AVX-512F/CD/BW/DQ/VL/IFMA/VBMI/VNNI/VBMI2/BITALG/VPOPCNTDQ/VPCLMULQDQ*/GFNI*/VAES*)

# ppc64le arch:
# VSX  (always available on Power8)
# VSX3 (always available on Power9)

# CPU_opt_SUPPORTED=ON/OFF - compiler support (possibly with additional flag)
# CPU_opt_IMPLIES=<list>
# CPU_opt_FORCE=<list> - subset of "implies" list
# CPU_opt_GROUP=<list> - similar to "implies" list, but additionally merges compiler flags
# CPU_opt_FLAGS_ON=""
# CPU_opt_FEATURE_ALIAS - mapping to CV_CPU_* HWFeature enum

# Input variables:
# CPU_BASELINE=<list> - preferred list of baseline optimizations
# CPU_DISPATCH=<list> - preferred list of dispatched optimizations

# Advanced input variables:
# CPU_BASELINE_REQUIRE=<list> - list of required baseline optimizations
# CPU_DISPATCH_REQUIRE=<list> - list of required dispatched optimizations
# CPU_BASELINE_DISABLE=<list> - list of disabled baseline optimizations

# Output variables:
# CPU_BASELINE_FINAL=<list> - final list of enabled compiler optimizations
# CPU_DISPATCH_FINAL=<list> - final list of dispatched optimizations
#
# CPU_DISPATCH_FLAGS_$opt - flags for source files compiled separately (<name>.avx2.cpp)
#
# CPU_opt_ENABLED_DEFAULT=ON/OFF - has compiler support without additional flag (CPU_BASELINE_DETECT=ON only)

set(CPU_ALL_OPTIMIZATIONS "SSE;SSE2;SSE3;SSSE3;SSE4_1;SSE4_2")
##list(APPEND CPU_ALL_OPTIMIZATIONS "AVX512_COMMON;AVX512_KNL;AVX512_KNM;AVX512_SKX;AVX512_CNL;AVX512_CEL;AVX512_ICL")
##list(APPEND CPU_ALL_OPTIMIZATIONS NEON VFPV3 FP16)
##list(APPEND CPU_ALL_OPTIMIZATIONS VSX VSX3)
list(REMOVE_DUPLICATES CPU_ALL_OPTIMIZATIONS)

ocv_update(CPU_VFPV3_FEATURE_ALIAS "")


set(HELP_CPU_BASELINE "Specify list of enabled baseline CPU optimizations")
set(HELP_CPU_BASELINE_REQUIRE "Specify list of required baseline CPU optimizations")
set(HELP_CPU_BASELINE_DISABLE "Specify list of forbidden baseline CPU optimizations")
set(HELP_CPU_DISPATCH "Specify list of dispatched CPU optimizations")
set(HELP_CPU_DISPATCH_REQUIRE "Specify list of required dispatched CPU optimizations")

foreach(var CPU_BASELINE CPU_BASELINE_REQUIRE CPU_BASELINE_DISABLE CPU_DISPATCH CPU_DISPATCH_REQUIRE)
  if(DEFINED $var)
    string(REPLACE "," ";" _list "$$var")
    set($var "$_list" CACHE STRING "$HELP_$var" FORCE)
  endif()
endforeach()

# process legacy flags
macro(ocv_optimization_process_obsolete_option legacy_flag OPT legacy_warn)
  if(DEFINED "$legacy_flag")
    if("$legacy_warn")
      message(STATUS "WARNING: Option $legacy_flag='$$legacy_flag' is deprecated and should not be used anymore")
      message(STATUS "         Behaviour of this option is not backward compatible")
      message(STATUS "         Refer to 'CPU_BASELINE'/'CPU_DISPATCH' CMake options documentation")
    endif()
    if("$$legacy_flag")
      if(NOT ";$CPU_BASELINE_REQUIRE;" MATCHES ";$OPT;")
        set(CPU_BASELINE_REQUIRE "$CPU_BASELINE_REQUIRE;$OPT" CACHE STRING "$HELP_CPU_BASELINE_REQUIRE" FORCE)
      endif()
    else()
      if(NOT ";$CPU_BASELINE_DISABLE;" MATCHES ";$OPT;")
        set(CPU_BASELINE_DISABLE "$CPU_BASELINE_DISABLE;$OPT" CACHE STRING "$HELP_CPU_BASELINE_DISABLE" FORCE)
      endif()
    endif()
  endif()
endmacro()
ocv_optimization_process_obsolete_option(ENABLE_SSE SSE ON)
ocv_optimization_process_obsolete_option(ENABLE_SSE2 SSE2 ON)
ocv_optimization_process_obsolete_option(ENABLE_SSE3 SSE3 ON)
ocv_optimization_process_obsolete_option(ENABLE_SSSE3 SSSE3 ON)
ocv_optimization_process_obsolete_option(ENABLE_SSE41 SSE4_1 ON)
ocv_optimization_process_obsolete_option(ENABLE_SSE42 SSE4_2 ON)
# ocv_optimization_process_obsolete_option(ENABLE_POPCNT POPCNT ON)
# ocv_optimization_process_obsolete_option(ENABLE_AVX AVX OFF)
# ocv_optimization_process_obsolete_option(ENABLE_AVX2 AVX2 OFF)
#ocv_optimization_process_obsolete_option(ENABLE_FMA3 FMA3 ON)

# ocv_optimization_process_obsolete_option(ENABLE_VFPV3 VFPV3 OFF)
# ocv_optimization_process_obsolete_option(ENABLE_NEON NEON OFF)

# ocv_optimization_process_obsolete_option(ENABLE_VSX VSX ON)

macro(ocv_is_optimization_in_list resultvar check_opt)
  set(__checked "")
  set(__queue $ARGN)
  set($resultvar 0)
  while(__queue AND NOT $resultvar)
    list(REMOVE_DUPLICATES __queue)
    set(__queue_current $__queue)
    set(__queue "")
    foreach(OPT $__queue_current)
      if("x$OPT" STREQUAL "x$check_opt")
        set($resultvar 1)
        break()
      elseif(NOT ";$__checked;" MATCHES ";$OPT;")
        list(APPEND __queue $CPU_$OPT_IMPLIES)
      endif()
      list(APPEND __checked $OPT)
    endforeach()
  endwhile()
endmacro()

macro(ocv_is_optimization_in_force_list resultvar check_opt)
  set(__checked "")
  set(__queue $ARGN)
  set($resultvar 0)
  while(__queue AND NOT $resultvar)
    list(REMOVE_DUPLICATES __queue)
    set(__queue_current $__queue)
    set(__queue "")
    foreach(OPT $__queue_current)
      if(OPT STREQUAL "$check_opt")
        set($resultvar 1)
        break()
      elseif(NOT ";$__checked;" MATCHES ";$OPT;")
        list(APPEND __queue $CPU_$OPT_FORCE)
      endif()
      list(APPEND __checked $OPT)
    endforeach()
  endwhile()
endmacro()

macro(ocv_append_optimization_flag var OPT)
  if(CPU_$OPT_FLAGS_CONFLICT)
    string(REGEX REPLACE " $CPU_$OPT_FLAGS_CONFLICT" "" $var " $$var ")
    string(REGEX REPLACE "^ +" "" $var "$$var")
  endif()
  set($var "$$var $CPU_$OPT_FLAGS_ON")
endmacro()

# Support GCC -march=native or Intel Compiler -xHost flags
if(";$CPU_BASELINE;" MATCHES ";NATIVE;" OR ";$CPU_BASELINE;" MATCHES ";HOST;")
  set(CPU_BASELINE_DETECT ON)
  set(_add_native_flag ON)
elseif(";$CPU_BASELINE;" MATCHES ";DETECT;")
  set(CPU_BASELINE_DETECT ON)
elseif(" $CMAKE_CXX_FLAGS " MATCHES " -march=native | -xHost | /QxHost ")
  if(DEFINED CPU_BASELINE)
    message(STATUS "CPU: Detected '-march=native' or '-xHost' compiler flag. Force CPU_BASELINE=DETECT.")
  endif()
  set(CPU_BASELINE "DETECT" CACHE STRING "$HELP_CPU_BASELINE")
  set(CPU_BASELINE_DETECT ON)
endif()

if(X86 OR X86_64)
  ocv_update(CPU_KNOWN_OPTIMIZATIONS "SSE;SSE2;SSE3;SSSE3;SSE4_1;SSE4_2")

  # ocv_update(CPU_AVX512_COMMON_GROUP "AVX_512F;AVX_512CD")
  # ocv_update(CPU_AVX512_KNL_GROUP "AVX512_COMMON;AVX512_KNL_EXTRA")
  # ocv_update(CPU_AVX512_KNM_GROUP "AVX512_KNL;AVX512_KNM_EXTRA;AVX_512VPOPCNTDQ")
  # ocv_update(CPU_AVX512_SKX_GROUP "AVX512_COMMON;AVX_512VL;AVX_512BW;AVX_512DQ")
  # ocv_update(CPU_AVX512_CNL_GROUP "AVX512_SKX;AVX_512IFMA;AVX_512VBMI")
  # ocv_update(CPU_AVX512_CEL_GROUP "AVX512_CNL;AVX_512VNNI")
  # ocv_update(CPU_AVX512_ICL_GROUP "AVX512_CEL;AVX_512VBMI2;AVX_512BITALG;AVX_512VPOPCNTDQ") # ? VPCLMULQDQ, GFNI, VAES

  ocv_update(CPU_SSE_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_sse.cpp")
  ocv_update(CPU_SSE2_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_sse2.cpp")
  ocv_update(CPU_SSE3_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_sse3.cpp")
  ocv_update(CPU_SSSE3_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_ssse3.cpp")
  ocv_update(CPU_SSE4_1_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_sse41.cpp")
  ocv_update(CPU_SSE4_2_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_sse42.cpp")
  # ocv_update(CPU_POPCNT_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_popcnt.cpp")
  # ocv_update(CPU_AVX_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx.cpp")
  # ocv_update(CPU_AVX2_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx2.cpp")
  # ocv_update(CPU_FP16_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_fp16.cpp")
  # ocv_update(CPU_AVX_512F_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512.cpp")
  # ocv_update(CPU_AVX512_COMMON_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512common.cpp")
  # ocv_update(CPU_AVX512_KNL_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512knl.cpp")
  # ocv_update(CPU_AVX512_KNM_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512knm.cpp")
  # ocv_update(CPU_AVX512_SKX_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512skx.cpp")
  # ocv_update(CPU_AVX512_CNL_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512cnl.cpp")
  # ocv_update(CPU_AVX512_CEL_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512cel.cpp")
  # ocv_update(CPU_AVX512_ICL_TEST_FILE "$OpenCV_SOURCE_DIR/cmake/checks/cpu_avx512icl.cpp")

  if(NOT OPENCV_CPU_OPT_IMPLIES_IGNORE)
    # ocv_update(CPU_AVX512_ICL_IMPLIES "AVX512_CEL")
    # ocv_update(CPU_AVX512_CEL_IMPLIES "AVX512_CNL")
    # ocv_update(CPU_AVX512_CNL_IMPLIES "AVX512_SKX")
    # ocv_update(CPU_AVX512_SKX_IMPLIES "AVX512_COMMON")
    # ocv_update(CPU_AVX512_KNM_IMPLIES "AVX512_KNL")
    # ocv_update(CPU_AVX512_KNL_IMPLIES "AVX512_COMMON")
    # ocv_update(CPU_AVX512_COMMON_IMPLIES "AVX_512F")
    # ocv_update(CPU_AVX_512F_IMPLIES "AVX2")
    # ocv_update(CPU_AVX_512F_FORCE "") # Don't force other optimizations
    # ocv_update(CPU_AVX2_IMPLIES "AVX;FMA3;FP16")
    # ocv_update(CPU_FMA3_IMPLIES "AVX2")
    # ocv_update(CPU_FMA3_FORCE "") # Don't force other optimizations
    # ocv_update(CPU_FP16_IMPLIES "AVX")
    # ocv_update(CPU_FP16_FORCE "") # Don't force other optimizations
    # ocv_update(CPU_AVX_IMPLIES "SSE4_2")
    ###ocv_update(CPU_SSE4_2_IMPLIES "SSE4_1;POPCNT")
    ocv_update(CPU_SSE4_2_IMPLIES "SSE4_1")
    ocv_update(CPU_POPCNT_IMPLIES "SSE4_1")
    ocv_update(CPU_POPCNT_FORCE "") # Don't force other optimizations
    ocv_update(CPU_SSE4_1_IMPLIES "SSE3;SSSE3")
    ocv_update(CPU_SSSE3_IMPLIES "SSE3")
    ocv_update(CPU_SSE3_IMPLIES "SSE2")
    ocv_update(CPU_SSE2_IMPLIES "SSE")
  endif()

  if(CV_ICC)
    macro(ocv_intel_compiler_optimization_option name unix_flags msvc_flags)
      ocv_update(CPU_$name_FLAGS_NAME "$name")
      if(MSVC)
        set(enable_flags "$msvc_flags")
        set(flags_conflict "/arch:[^ ]*|/Qx:[^ ]+")
      else()
        set(enable_flags "$unix_flags")
        set(flags_conflict "-msse[^ ]*|-mssse3|-mavx[^ ]*|-march[^ ]*|-x[^ ]+")
      endif()
      ocv_update(CPU_$name_FLAGS_ON "$enable_flags")
      if(flags_conflict)
        ocv_update(CPU_$name_FLAGS_CONFLICT "$flags_conflict")
      endif()
    endmacro()
    # ocv_intel_compiler_optimization_option(AVX2 "-march=core-avx2" "/arch:CORE-AVX2")
    # ocv_intel_compiler_optimization_option(FP16 "-mavx" "/arch:AVX")
    # ocv_intel_compiler_optimization_option(AVX "-mavx" "/arch:AVX")
    # ocv_intel_compiler_optimization_option(FMA3 "" "")
    # ocv_intel_compiler_optimization_option(POPCNT "" "")
    ocv_intel_compiler_optimization_option(SSE4_2 "-msse4.2" "/arch:SSE4.2")
    ocv_intel_compiler_optimization_option(SSE4_1 "-msse4.1" "/arch:SSE4.1")
    ocv_intel_compiler_optimization_option(SSE3 "-msse3" "/arch:SSE3")
    ocv_intel_compiler_optimization_option(SSSE3 "-mssse3" "/arch:SSSE3")
    ocv_intel_compiler_optimization_option(SSE2 "-msse2" "/arch:SSE2")
    if(NOT X86_64) # x64 compiler doesn't support /arch:sse
      ocv_intel_compiler_optimization_option(SSE "-msse" "/arch:SSE")
    endif()
    # ocv_intel_compiler_optimization_option(AVX_512F "-xCOMMON-AVX512" "/Qx:COMMON-AVX512")
    # ocv_intel_compiler_optimization_option(AVX512_COMMON "-xCOMMON-AVX512" "/Qx:COMMON-AVX512")
    # ocv_intel_compiler_optimization_option(AVX512_KNL "-xKNL" "/Qx:KNL")
    # ocv_intel_compiler_optimization_option(AVX512_KNM "-xKNM" "/Qx:KNM")
    # ocv_intel_compiler_optimization_option(AVX512_SKX "-xSKYLAKE-AVX512" "/Qx:SKYLAKE-AVX512")
    # ocv_intel_compiler_optimization_option(AVX512_CNL "-xCANNONLAKE" "/Qx:CANNONLAKE")
    # ocv_intel_compiler_optimization_option(AVX512_CEL "-xCASCADELAKE" "/Qx:CASCADELAKE")
    # ocv_intel_compiler_optimization_option(AVX512_ICL "-xICELAKE-CLIENT" "/Qx:ICELAKE-CLIENT")
  elseif(CV_GCC OR CV_CLANG)
    # ocv_update(CPU_AVX2_FLAGS_ON "-mavx2")
    # ocv_update(CPU_FP16_FLAGS_ON "-mf16c")
    # ocv_update(CPU_AVX_FLAGS_ON "-mavx")
    # ocv_update(CPU_FMA3_FLAGS_ON "-mfma")
    # ocv_update(CPU_POPCNT_FLAGS_ON "-mpopcnt")
    ocv_update(CPU_SSE4_2_FLAGS_ON "-msse4.2")
    ocv_update(CPU_SSE4_1_FLAGS_ON "-msse4.1")
    ocv_update(CPU_SSE3_FLAGS_ON "-msse3")
    ocv_update(CPU_SSSE3_FLAGS_ON "-mssse3")
    ocv_update(CPU_SSE2_FLAGS_ON "-msse2")
    ocv_update(CPU_SSE_FLAGS_ON "-msse")
    # if(NOT (CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0"))  # GCC >= 5.0
      # # ocv_update(CPU_AVX_512F_FLAGS_ON "-mavx512f")
      # # ocv_update(CPU_AVX_512CD_FLAGS_ON "-mavx512cd")
      # # ocv_update(CPU_AVX512_KNL_EXTRA_FLAGS_ON "-mavx512er -mavx512pf")
      # # ocv_update(CPU_AVX512_KNM_EXTRA_FLAGS_ON "-mavx5124fmaps -mavx5124vnniw")
      # # ocv_update(CPU_AVX_512BW_FLAGS_ON "-mavx512bw")
      # # ocv_update(CPU_AVX_512DQ_FLAGS_ON "-mavx512dq")
      # # ocv_update(CPU_AVX_512VL_FLAGS_ON "-mavx512vl")
      # # ocv_update(CPU_AVX_512IFMA_FLAGS_ON "-mavx512ifma")
      # # ocv_update(CPU_AVX_512VBMI_FLAGS_ON "-mavx512vbmi")
      # # ocv_update(CPU_AVX_512VNNI_FLAGS_ON "-mavx512vnni")
      # # ocv_update(CPU_AVX_512VBMI2_FLAGS_ON "-mavx512vbmi2")
      # # ocv_update(CPU_AVX_512BITALG_FLAGS_ON "-mavx512bitalg")
      # # ocv_update(CPU_AVX_512VPOPCNTDQ_FLAGS_ON "-mavx512vpopcntdq")
    # else()
      # ocv_update(CPU_AVX_512F_SUPPORTED OFF)
    # endif()
  elseif(MSVC)
    # ocv_update(CPU_AVX2_FLAGS_ON "/arch:AVX2")
    # ocv_update(CPU_AVX_FLAGS_ON "/arch:AVX")
    # ocv_update(CPU_FP16_FLAGS_ON "/arch:AVX")
    if(NOT MSVC64)
      # 64-bit MSVC compiler uses SSE/SSE2 by default
      ocv_update(CPU_SSE_FLAGS_ON "/arch:SSE")
      ocv_update(CPU_SSE_SUPPORTED ON)
      ocv_update(CPU_SSE2_FLAGS_ON "/arch:SSE2")
      ocv_update(CPU_SSE2_SUPPORTED ON)
    else()
      ocv_update(CPU_SSE_SUPPORTED ON)
      ocv_update(CPU_SSE2_SUPPORTED ON)
      # ocv_update(CPU_AVX_512F_FLAGS_ON "/arch:AVX512")
    endif()
    # Other instruction sets are supported by default since MSVC 2008 at least
  else()
    message(WARNING "TODO: Unsupported compiler")
  endif()

  # if(NOT DEFINED CPU_DISPATCH)
    # if(X86_64)
      # set(CPU_DISPATCH "SSE4_1;SSE4_2;AVX;FP16;AVX2;AVX512_SKX" CACHE STRING "$HELP_CPU_DISPATCH")
    # else()
      # set(CPU_DISPATCH "SSE4_1;SSE4_2;AVX;FP16" CACHE STRING "$HELP_CPU_DISPATCH")
    # endif()
  # endif()

  if(NOT DEFINED CPU_BASELINE)
    if(APPLE)
      # MacOS X has limited set of possible supported H/W, so compiler is configured well
      set(CPU_BASELINE "DETECT" CACHE STRING "$HELP_CPU_BASELINE")
    elseif(X86_64)
      set(CPU_BASELINE "SSE3" CACHE STRING "$HELP_CPU_BASELINE")
    else()
      set(CPU_BASELINE "SSE2" CACHE STRING "$HELP_CPU_BASELINE")
    endif()
  endif()

之后,您只需使用 CMake 构建 OpenCV。这是我的做法:

    进入opencv-4.1.1文件夹,新建一个文件夹,命名为build 打开命令提示符或 PowerShell 窗口并导航到 build 文件夹(或只需按 Shif+右键单击 并选择在此处打开 PowerShell 窗口)

    执行以下命令(我已禁用构建示例并进行测试,因为其中一些会失败)(不要忘记命令末尾的 ..!)(对于 Visual Studio 2019 替换 "Visual Studio 15 2017""Visual Studio 16 2019"):

    cmake -D ENABLE_AVX=OFF -D ENABLE_AVX2=OFF -D BUILD_EXAMPLES=OFF -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -G "Visual Studio 15 2017" ..

    CMake 生成项目后,使用 Visual Studio 打开 build 文件夹中的解决方案文件 (OpenCV.sln)

    构建 ALL_BUILD 项目

    一个项目将失败。如果您查看错误,它会显示类似 'CV_CPU_HAS_SUPPORT_AVX512_SKX': undeclared identifier 只需将代码中的关键字 CV_CPU_HAS_SUPPORT_AVX512_SKX 替换为 false(它会出现在整个解决方案中 3 次,替换所有 3) 并再次点击 build on ALL_BUILD

    构建 INSTALL 项目。它将在我们的构建文件夹中创建一个名为 install 的文件夹。

    使用您项目中 install 文件夹中新构建的 OpenCV 库、二进制文件和包含文件,它应该可以工作。

【讨论】:

谢谢。效果很好。 或者只是移民到 Linux。 英特尔的脏手。

以上是关于使用 cmake 制作不支持 sse 的库(windows 版本)的主要内容,如果未能解决你的问题,请参考以下文章

linux系统2022intel oneapi的mkl库需要指定一下是吗

请问如何让QT程序支持XP-WIN10系统

如何提供支持静态链接的 cmake 包(又名 Config.cmake)

我如何制作 sigpack 来找到犰狳?

使用 SSE4.2 编译 TensorFlow,AVX 因 bazel 失败

在不使用 CTest 的情况下使用 CMake 构建测试