Linking Opencv in a project using cmake

10,020

I guess that you install opencv using apt-get install, tell me if I am wrong.

Firstly show opencvConfig.cmake variables such as OpenCV_INCLUDE_DIRS or OpenCV_LIBS.

you can use this code:

  cmake_minimum_required(VERSION 2.8)
  project(five-point-nister)
  SET("OpenCV_DIR" "/usr/share/OpenCV")
  find_package( OpenCV REQUIRED )

  MESSAGE(STATUS "Include dirs ${OpenCV_INCLUDE_DIRS}")
  MESSAGE(STATUS "LINK LIBRARIES ${OpenCV_LIBS}")

  add_executable(five-point-nister
  five-point.cpp precomp.cpp modelest.cpp)
  target_link_libraries(five-point-nister ${OpenCV_LIBS})

These variables are located on your OpencvConfig.cmake, su as:

#    This file will define the following variables:
#      - OpenCV_LIBS                     : The list of all imported targets for OpenCV modules.
#      - OpenCV_INCLUDE_DIRS             : The OpenCV include directories.
#      - OpenCV_COMPUTE_CAPABILITIES     : The version of compute capability.
#      - OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API.
#      - OpenCV_VERSION                  : The version of this OpenCV build: "2.4.9"
#      - OpenCV_VERSION_MAJOR            : Major version part of OpenCV_VERSION: "2"
#      - OpenCV_VERSION_MINOR            : Minor version part of OpenCV_VERSION: "4"
#      - OpenCV_VERSION_PATCH            : Patch version part of OpenCV_VERSION: "9"
#      - OpenCV_VERSION_TWEAK            : Tweak version part of OpenCV_VERSION: "0"

However, I would recommend some tips:

use cmake-gui to define you OPENCV_DIR directory, because of your static directory path.

  • Don't use apt-get install openCV, sometimes are not enough updated.

  • Try to compile opencv or download prebuilt files http://opencv.org/downloads.html.

  • Compile opencv and set on your app project your OPENCV_DIR in the build directory where you compiled previously.

Cheers.

Share:
10,020
batuman
Author by

batuman

Updated on June 14, 2022

Comments

  • batuman
    batuman almost 2 years

    I have two opencv installed, one is in /usr/local and another one in /usr. So /usr/share/OpenCV has OpencvConfig.cmake for Opencv2.4.10 and /usr/local/share/OpenCV has OpencvConfig.cmake for Opencv3.1.0. In my project's cmake I set the path for OPencv2.4.10 as

    cmake_minimum_required(VERSION 2.8)
    project(five-point-nister)
    
    SET("OpenCV_DIR" "/usr/share/OpenCV")
    find_package( OpenCV REQUIRED )
    
    add_executable(five-point-nister
        five-point.cpp precomp.cpp modelest.cpp)
    target_link_libraries(five-point-nister ${OpenCV_LIBS})
    

    But when I compile the project, the project is trying to link to OPencv3.1.0 and the errors are

    In file included from /usr/local/include/opencv2/core.hpp:59:0,
                     from /usr/local/include/opencv2/calib3d.hpp:47,
                     from /usr/local/include/opencv2/calib3d/calib3d.hpp:48,
    

    By right, it should be /usr/include/opencv2, but now is /usr/local/include/opencv2. So my project is still linking to Opencv3.1.0. How can I correct it? Thanks