c++opencvcmakeillegal-instruction

using cmake to make a library without sse support (windows version)


I want to setup OpenCV using CMake for Visual Studio 2019. After all these processes while debugging my program I get this error:

Unhandled exception at 0x00007FFCF1FF2382 (opencv_world412d.dll) in yaabolfazl.exe: 0xC000001D: Illegal Instruction. occurred.

I think it is caused by my processor which can not support AVX (Windows 8.1, core i5 M520 2.4GHz). (Am I right?) I found out I can make a new version of OpenCV using CMake without AVX support by using CPU_DISPATCH and CPU_BASELINE option, but when I change their value I get this error:

Error in the configuration process, project file may be invalid.

So how can I build OpenCV using CMake without AVX support(e.g. using SSE support only)?

My CMake configuration My cmake configuration

CPU instruction set: CPU-Z


Solution

  • The problem most likely is that your CPU doesn't support some CPU features (like AVX) that OpenCV uses. I'm not sure what are the exact CMake flags to disable these features when building OpenCV, but there is another way to solve this (the brute-force way :D). Download the OpenCV sources from this link and unzip it (the current latest version is 4.1.1). Go to the opencv-4.1.1\cmake folder and open OpenCVCompilerOptimizations.cmake in a text editor. It contains optimizations for different CPU architecture. The screenshot attached in the question shows the supported instructions by your CPU. In the OpenCVCompilerOptimizations.cmake file simply remove (or comment out) every instruction options that are not supported by your CPU. Here is my version (I only include the first half of the file where the modifications are. Compare it with the original to see the differences):

    # 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()
    

    After that you simply build OpenCV using CMake. Here is how I did it:

    1. Go to the opencv-4.1.1 folder and create a new folder, let's name it build
    2. Open a Command Prompt or PowerShell window and navigate to the build folder (or simply press Shif+Right Click and select Open PowerShell Window here)
    3. Execute the following command (I've disabled building examples and test because some of them will fail) (don't forget the .. at the end of the command!) (for Visual studio 2019 replace "Visual Studio 15 2017" with "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" ..

    4. After CMake finishes generating the project, open the solution file (OpenCV.sln) inside the build folder using Visual Studio

    5. Build the ALL_BUILD project

    6. One project will fail. If you look the error, it says something like 'CV_CPU_HAS_SUPPORT_AVX512_SKX': undeclared identifier Simply replace the keyword CV_CPU_HAS_SUPPORT_AVX512_SKX with false in the code (it appears 3 times in the whole solution, replace all 3) and hit build on ALL_BUILD again

    7. Build the INSTALL project. It will create a folder called install inside our build folder.

    8. Use the freshly built OpenCV libraries, binaries and includes from the install folder in you project and it should work.