使用cmake在项目中链接Opencv

时间:2016-06-08 01:29:53

标签: c++ opencv cmake

我安装了两个opencv,一个在/ usr / local,另一个在/ usr。 因此/ usr / share / OpenCV具有Opencv2.4.10的OpencvConfig.cmake和/ usr / local / share / OpenCV具有Opencv3.1.0的OpencvConfig.cmake。 在我的项目cmake中,我将OPencv2.4.10的路径设置为

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})

但是当我编译项目时,该项目正试图链接到OPencv3.1.0并且错误是

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,

右边应该是/usr/include/opencv2,但现在是/usr/local/include/opencv2。所以我的项目仍然链接到Opencv3.1.0。 我该如何纠正? 感谢

1 个答案:

答案 0 :(得分:2)

我猜你用apt-get install安装opencv,告诉我是不是错了。

首先显示opencvConfig.cmake变量,例如OpenCV_INCLUDE_DIRS或OpenCV_LIBS。

您可以使用此代码:

  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})

这些变量位于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"

但是,我会推荐一些提示:

使用cmake-gui定义OPENCV_DIR目录,因为你的静态目录路径。

  • 不要使用apt-get install openCV,有时候更新不够。

  • 尝试编译opencv或下载预建文件http://opencv.org/downloads.html

  • 编译opencv并在您的应用项目中设置您之前编译的构建目录中的OPENCV_DIR。

干杯。