Using cmake to find OpenCV version
I'm trying to update the cmakefile for another project that depends on OpenCV. What's a good way to identify the correct OpenCV version across multiple platforms? I just need to figure out if OpenCV 3 is available on the current machine or not. Any hints or ideas on how to do this would be useful!
Here's a summary of what I'm trying to accomplish:
- run
locate libopencv_imgcodecs | head -n 1 | wc -l
- If this command returns 0, assume OpenCV is installed (I know this is potentially a bad assumption)
- If the command returns a non-zero number, set OpenCV version to 3.
- Set the variable
OPENCV_VERSION
based on this.
I don't feel too good about running the shell command (locate, etc).. is there a better way to do this?
IMHO you should
find_package(OpenCV REQUIRED)
if(NOT "${OPENCV_VERSION}" STREQUAL "3")̀ ...
For better understanding you can print the version with
message(OPENCV_VERSION)
find_package
requires this additional filecode.opencv.org/attachments/196/FindOpenCV.cmake
. This requires that I set the variableOpenCV_DIR
. Is there a way to locate OpenCV without setting this variable?I suppose It is hard to do a CMake check if you did not installed the lib with CMake... does this help you?
This works for me, with different capitalization of the variable: message("OpenCV version: " ${OpenCV_VERSION})