在cmake工程中使用opencv需要在CMakeLists.txt文件中加以调用,在opencv2.xx版本,可以用以下语句
# 寻找OpenCV库
find_package( OpenCV REQUIRED )
# 添加头文件
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( xxx xxx.cpp )
# 链接OpenCV库
target_link_libraries( xxx ${OpenCV_LIBS} )
但如果你使用的是OpenCV 3.x版本,再用上面的方法就会报错
CMake Warning at /home/zn/opencv-3.1.0/cmake/OpenCVConfig.cmake:166 (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:11 (find_package) CMake Error at CMakeLists.txt:11 (find_package): Found package configuration file: /home/zn/opencv-3.1.0/cmake/OpenCVConfig.cmake but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be NOT FOUND. -- Configuring incomplete, errors occurred!
从上面的报错可以看出在OpenCVConfig.cmake文件中设置OpenCV_FOUND to FALSE,故find_package( OpenCV REQUIRED )是不可用的。我认为这是opencv3.x版本对opencv2.x版本的屏蔽,以防混用。要使用opencv3.x需要用如下语句
# 寻找OpenCV库 我的版本的opencv3.1.0 find_package( OpenCV 3.1 REQUIRED ) # 添加头文件 include_directories( ${OpenCV_INCLUDE_DIRS} ) add_executable( xxx xxx.cpp ) # 链接OpenCV库 target_link_libraries( xxx ${OpenCV_LIBS} )
也就是要加上opencv对应的版本号就可以解决报错了。