Clion, CMake, Ninja, OpenCV, Windows Pack No Binaries Compatible

错误信息 Error Info

Found OpenCV Windows Pack but it has no binaries compatible with your configuration.
You should manually point CMake variable OpenCV_DIR to your build of OpenCV library.

该错误是在使用Clion配置OpenCV 4.5.5的过程中出现的。直接原因是OpenCV的CMake文件没有识别到正确的CPU架构和运行时版本,比如笔者在Windows-11/Intel-12700K/VS-2022平台上识别为32位的x86和vc14(2015)版本。

解决方案:

  1. 修改CMake配置。Clion->File->Settings>Build,Execution,Deployment->CMake->Generator->(笔者为Visual Studio 17 2022)。实测确实Ninja快。
  2. 在CMake文件引用OpenCVConfig.cmake文件前预定义变量OpenCV_ARCH和OpenCV_RUNTIME。变量值根据自身条件来定,比如笔者用默认的OpenCV发行版,就定义为"x64"和"vc15"。
set(OpenCV_ARCH "x64")
set(OpenCV_RUNTIME vc15)

具体原因

  1. OpenCV CMake文件配置了错误的OpenCV_ARCH和OpenCV_RUNTIME变量。
  2. 配置错误的原因是CMAKE_OpenCVConfig.cmake文件中依赖CMake工具的CMAKE_GENERATOR变量定义CPU架构、编译器和运行时。这里${CMAKE_GENERATOR}的值是"Ninja"。具体代码如下:
  if("${CMAKE_GENERATOR}" MATCHES "(Win64|IA64)")
    set(OpenCV_ARCH "x64")
  elseif("${CMAKE_GENERATOR_PLATFORM}" MATCHES "ARM64")
    set(OpenCV_ARCH "ARM64")
  elseif("${CMAKE_GENERATOR}" MATCHES "ARM")
    set(OpenCV_ARCH "ARM")
  elseif("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
    set(OpenCV_ARCH "x64")
  else()
    set(OpenCV_ARCH x86)
  endif()
  1. 最终原因是Clion默认使用Ninja控制CMake编译流程,即cmake -G调用了Ninja导致CMAKE_GENERATOR变量显示为Ninja,进而导致CMake解析得到的CMAKE_GENERATOR变量值不包含Win64等字段。
参考二楼老哥的提示 Error configuring OpenCV project with CMake on Windows

你可能感兴趣的:(Clion, CMake, Ninja, OpenCV, Windows Pack No Binaries Compatible)