背景
由于不同的CMake工程需要使用不同的OpenCV版本,所以需要指定OpenCV。CMake查找OpenCV通过寻找OpenCVConfig.cmake文件进行。
步骤
cmake_minimum_required(VERSION 2.8)
project(test)
set( CMAKE_BUILD_TYPE "Debug" )
set( CMAKE_CXX_FLAGS "-std=c++11 -O0" )
# 禁用CUDA
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
# 指定OpenCVConfig.cmake目录
set(OpenCV_DIR /media/hdd/opencv-2.3.0/build)
#or set(OpenCV_DIR /usr/local/opencv2.3/share/OpenCV)
find_package(OpenCV REQUIRED)
add_executable(test test.cpp)
# opencv libraries
target_link_libraries( test ${OpenCV_LIBS})
# 输出真正的版本是不是自己所指定想要的版本
MESSAGE(STATUS "OpenCV version is " ${OpenCV_VERSION})
问题
由于自己的find_package中指定了OpenCV 2.3版本时:
find_package(OpenCV 2.3 REQUIRED)
此时的OpenCV版本输出为2.4
总是达不到自己想要的效果,指定一个5.0高版本时,发现报错:
find_package(OpenCV 5.0 REQUIRED)
CMake Error at CMakeLists.txt:12 (find_package):
Could not find a configuration file for package "OpenCV" that is compatible
with requested version "5.0".
The following configuration files were considered but not accepted:
/media/hdd/opencv-2.3.0/build/OpenCVConfig.cmake, version: unknown
/usr/local/opencv2.3/share/opencv/OpenCVConfig.cmake, version: unknown
/usr/local/opencv2.3/share/OpenCV/OpenCVConfig.cmake, version: unknown
/usr/local/opencv2.4/share/OpenCV/OpenCVConfig.cmake, version: 2.4.13.6
这时才知道原来自己的OpenCV 2.3版本在CMake中是没有被发现的。
所以更改策略,不加指定的版本。
find_package(OpenCV REQUIRED)
网友评论