You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CLion中CMake配置OpenCV库遇兼容错误,无法构建项目

问题:CLion中OpenCV CMake构建失败,提示无兼容二进制文件

我的CMake配置文件

cmake_minimum_required(VERSION 3.28)
project(AICS)
include_directories(include)
include_directories(src)
set(CMAKE_CXX_STANDARD 17)

set(OpenCV_DIR "C:/opencv/build")
find_package( OpenCV REQUIRED )

add_executable(AICS main.cpp
        src/MyScreen.cpp
        include/MyScreen.h
        include/Scripts.h
        src/Scripts.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

include_directories("C:/opencv/build/include")

构建错误信息

CMake Warning at C:/opencv/build/OpenCVConfig.cmake:190 (message):
  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.
Call Stack (most recent call first):
  CMakeLists.txt:9 (find_package)


CMake Error at CMakeLists.txt:9 (find_package):
  Found package configuration file:

    C:/opencv/build/OpenCVConfig.cmake

  but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
  NOT FOUND.


-- Configuring incomplete, errors occurred!

已尝试但无效的方案

  • 添加set(OpenCV_FOUND 1)语句,无效果
  • 使用不同版本的OpenCV
  • 删除find_package语句,项目可构建但运行时出现undefined reference to cv::VideoCapture::VideoCapture()等OpenCV类型未定义引用错误
  • 删除CMakeCache.txt文件,无效果

有效解决方法

1. 匹配OpenCV与编译器架构

  • 查看CLion当前使用的编译器架构:打开File > Settings > Build, Execution, Deployment > Toolchains,确认所选编译器是x86(32位)还是x64(64位)
  • 确保OpenCV预编译包与编译器架构一致:OpenCV官方Windows安装包默认会在C:/opencv/build下生成x86和x64子目录,对应不同架构,子目录里的vcxx(比如vc17对应VS2022,vc16对应VS2019)则对应Visual Studio版本

2. 修正OpenCV_DIR路径

把set(OpenCV_DIR)指向对应架构和VS版本的lib目录,比如用64位VS2022的话,修改为:

set(OpenCV_DIR "C:/opencv/build/x64/vc17/lib")

3. 优化CMake配置(推荐)

  • 用target_include_directories替代全局的include_directories,更符合现代CMake规范,同时自动导入OpenCV的头文件:
    # 替换原来的include_directories(include)和include_directories(src)
    target_include_directories(AICS PRIVATE include src ${OpenCV_INCLUDE_DIRS})
    
  • 移除手动添加的include_directories("C:/opencv/build/include"),因为find_package(OpenCV REQUIRED)会自动处理OpenCV的头文件路径

4. 清理后重新构建

  • 删除项目下的cmake-build-debug或cmake-build-release目录
  • 重启CLion,重新执行CMake配置和构建

内容的提问来源于stack exchange,提问作者Aparkin Matvey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:12:43