[CMake] Using different installs/versions of the OpenCV library

Bart Vandewoestyne Bart.Vandewoestyne at telenet.be
Thu Feb 14 10:48:07 EST 2013


On 02/14/2013 04:35 PM, Andreas Stahl wrote:
> Hello Bart,
>
> have you tried setting OpenCV_DIR before calling find_package()? I'm
> basing this on http://opencv.willowgarage.com/wiki/FindOpenCV.cmake
> Most package <PKG>Find.cmake scripts look if <PKG>_DIR is set to a valid
> directory and look there first for all the libraries before checking
> default install paths. You would use it like this:
>
> if(USE_TMP_OPENCV)
> set(OpenCV_DIR /tmp/opencv_install)
> elseif(USE_FAT_OPENCV)
> set(OpenCV_DIR "/bla/bla/bla/opencv")
> endif()
>
> find_package(OpenCV REQUIRED)

I quickly tested this, but seems not to work.  Currently, this is my 
best solution:

-----------------------------------------------------------------
# Set this to the version of OpenCV that you need.
set(OPENCV_LIB "latest")

if(OPENCV_LIB STREQUAL "latest")

   message(STATUS "Using my own compiled OpenCV.")
   include_directories("/opt/opencv/latest/include/opencv")
   link_directories("/opt/opencv/latest/lib")
   set(OpenCV_LIBS 
"opencv_gpu;opencv_contrib;opencv_legacy;opencv_objdetect;opencv_calib3d;opencv_features2d;opencv_video;opencv_highgui;opencv_ml;opencv_imgproc;opencv_flann;opencv_core")

elseif(OPENCV_LIB STREQUAL "fat")

   message(STATUS "Using OpenCV from foobar repository.")
   include_directories("$ENV{HOME}/foo/bar)
   link_directories("$ENV{HOME}/foo/bar")
   set(OpenCV_LIBS 
"opencv_gpu;opencv_contrib;opencv_legacy;opencv_objdetect;opencv_calib3d;opencv_features2d;opencv_video;opencv_highgui;opencv_ml;opencv_imgproc;opencv_flann;opencv_core")

elseif(OPENCV_LIB STREQUAL "system")

   message(STATUS "Using OpenCV that got automatically detected.")
   find_package( OpenCV REQUIRED )

else()
   message(ERROR "No OpenCV library found!")
endif()
------------------------------------------------------------


Not that clean, but works well enough for now :-)  I'm always interested 
in suggestions for making this shorter, cleaner, more elegant,...

Regards,
Bart


More information about the CMake mailing list