不确定如何将 opencv 库添加到 CMakeLists.txt

Posted

技术标签:

【中文标题】不确定如何将 opencv 库添加到 CMakeLists.txt【英文标题】:Not sure how to add opencv library to CMakeLists.txt 【发布时间】:2011-07-07 09:51:09 【问题描述】:

我希望有人可以帮助我。

我有一个简单的 CMakeLists.txt 以便在 Ubuntu 上构建我的项目。我正在使用 CMake 2.8.1,目前这是代码:

cmake_minimum_required(VERSION 2.4.6)
set(CMAKE_MODULE_PATH $CMAKE_MODULE_PATH /home/user/workspace)
include($ENVROS_ROOT/core/rosbuild/rosbuild.cmake)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

rosbuild_init()
find_package(OpenCV 2) 

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH $PROJECT_SOURCE_DIR/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH $PROJECT_SOURCE_DIR/lib)

#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()

#common commands for building c++ executables and libraries
#rosbuild_add_library($PROJECT_NAME src/example.cpp)
#target_link_libraries($PROJECT_NAME another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost($PROJECT_NAME thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example $PROJECT_NAME)

include_directories($CMAKE_SOURCE_DIR/include
                                  $OpenCV_INCLUDE_DIRS)

rosbuild_add_executable (RosPub src/paste.cpp) target_link_libraries (RosPub openni_driver usb-1.0 $OpenCV_LIBS)

我需要在我的项目中添加 opencv 库。我已经添加了它们,但我仍然无法让我的代码工作。它不断向我发送此错误:

‘struct MyOpenNIExample::ImgContext’没有名为‘image’的成员

其中有几个。

在我添加 find_package(OpenCV REQUIRED 到 CMakeLists.txt 之后, 我得到这个错误

Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
   directory containing a CMake configuration file for OpenCV.  The file will
   have one of the following names:

     OpenCVConfig.cmake
     opencv-config.cmake

我该怎么办?我正在使用 Apple 并使用 Ubuntu 10.04。

因为我需要

#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"

我加了

      find_package(OpenCV 2), 
 set(CMAKE_MODULE_PATH $CMAKE_MODULE_PATH /home/user/workspace) and 


    include_directories($CMAKE_SOURCE_DIR/include
                                      $OpenCV_INCLUDE_DIRS)
    target_link_libraries (RosPub openni_driver usb-1.0 $OpenCV_LIBS)

【问题讨论】:

【参考方案1】:
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp" 
#include "opencv2\imgproc\imgproc.hpp" 

上面的包含文件包含在 ROS 的 vision_opencv 中。所以要包含它,请在清单文件中添加 opencv 依赖项。

这会有所帮助。

【讨论】:

【参考方案2】:
    从上面的链接中获取 FindOpenCV.cmake 并将其放在计算机上的任何位置。 在 cmake_minimum_required 之后添加行 set(CMAKE_MODULE_PATH $CMAKE_MODULE_PATH [放置 FindOpenCV.cmake 的文件夹路径]) 将 find_package(OpenCV) 添加到您的 CMakeLists.txt 中 在此步骤中,您可以检查 CMakeLists.txt 中的 OpenCV_FOUND 和其他 OpenCV 变量

【讨论】:

我的意思是来自@spraff answer的链接 感谢您的帮助。是添加类似的行: set(CMAKE_MODULE_PATH $CMAKE_MODULE_PATH [/media/Data/workspace/Lakshmen]) shld 我包含 find_package[OpenCV required] btw,我试图包含的头文件是#include 我有这个错误:未找到 OpenCV。请使用 OpenCV_DIR 环境指定 OpenCV 目录。变量 太棒了。然后尝试指定 OpenCV_DIR 环境变量。对于 linux,它可以直接从命令行完成 - "OpenCV_DIR=[path to installed OpenCV] cmake [path to your CMakeLists.txt]"

以上是关于不确定如何将 opencv 库添加到 CMakeLists.txt的主要内容,如果未能解决你的问题,请参考以下文章

如何将库添加到我的.jar文件中

将 opencv 库添加到 QT 创建者并出现错误:链接器命令失败,退出代码为 1

包含另一个 CMake 项目作为库

如何在 IntelliJ IDEA 中添加“库项目”?

将静态库添加到 C 或 C++ 项目的常用方法是啥?

如何将“aar”库添加到 AOSP?