gpt4 book ai didi

c++ - 使用 cmake 在项目中链接 Opencv

转载 作者:太空狗 更新时间:2023-10-29 20:54:53 24 4
gpt4 key购买 nike

我安装了两个opencv,一个在/usr/local,另一个在/usr。所以/usr/share/OpenCV 有 OpencvConfig.cmake 用于 Opencv2.4.10 和/usr/local/share/OpenCV 有 OpencvConfig.cmake 用于 Opencv3.1.0。在我项目的 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。我该如何纠正它?谢谢

最佳答案

我猜你是使用 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 中,如:

#    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。

干杯。

关于c++ - 使用 cmake 在项目中链接 Opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com