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?
find_library (<VAR> name1 [path1 path2 ...]) This command is used to find a library. A cache entry, or a normal variable if NO_CACHE is specified, named by <VAR> is created to store the result of this command.
For CMake, these sections are called cmake-modules and can be found in the Modules subdirectory of your installation.
On UNIX platforms the user package registry is stored in the user home directory under ~/. cmake/packages. A <package> may appear under the directory: ~/. cmake/packages/<package> as a file, with arbitrary name, whose content specifies the directory containing the package configuration file.
You access a variable by using ${} , such as ${MY_VARIABLE} . 1. CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.
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()
OpenCV provides a built in constant for this:
CV_MAJOR_VERSION
Using this constant you're able to easily write version dependent code.
#if CV_MAJOR_VERSION >= 3
//OpenCV 3.x code
#else
//OpenCV 2.4.x code
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With