Issue
I'm currently developing a C++ project that uses CMake files and OpenCV among other libraries. My target would be to be able to compile my code both with version 2.4.something and with 3.0.
In order to do so, I thought of using CMake configured to set a variable indicating whether the OpenCV package found in the configuration phase has a version greater or equal to 3.0. Using this variable I can then include or exclude ad-hoc parts of my code.
However I was not able to find anywhere how can I know in a CMake file the version of a found package.
The pseudo code of my CMake file would look something like this:
....
find_package(OpenCV 2.4 REQUIRED)
if(OpenCV_Version >= 3)
set (OpenCV_3 1)
else
set (OpenCV_3 0)
endif(OpenCV_Version)
....
Is it possible to do this or am I doing something wrong?
Solution
From CMake documentation on find_package:
If the version is acceptable the following variables are set:
<package>_VERSION
full provided version string
<package>_VERSION_MAJOR
major version if provided, else 0
<package>_VERSION_MINOR
minor version if provided, else 0
<package>_VERSION_PATCH
patch version if provided, else 0
<package>_VERSION_TWEAK
tweak version if provided, else 0
<package>_VERSION_COUNT
number of version components, 0 to 4
You may use either variable OpenCV_VERSION
with full version string for comparing using VERSION_*
modes of if()
command:
if(OpenCV_VERSION VERSION_LESS "3.0")
# 2.4 version
else()
# 3.0 version
endif()
or version-component variables with number comparision:
if(OpenCV_VERSION_MAJOR LESS 3)
# 2.4 version
else()
# 3.0 version
endif()
Answered By - Tsyvarev