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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6608697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:25:20  来源:igfitidea点击:

Not sure how to add opencv library to CMakeLists.txt

c++cmakeros

提问by laksh

I hope someone can help me.

我希望有一个人可以帮助我。

I have a simple CMakeLists.txt in order to build my project on Ubuntu. I'm using CMake 2.8.1 and at the moment this is the code:

我有一个简单的 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($ENV{ROS_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})

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

I need to add opencv libraries on my project. I have added them but i can't still get my code to work. its keeps posting me this error:

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

‘struct MyOpenNIExample::ImgContext' has no member named ‘image'

“struct MyOpenNIExample::ImgContext”没有名为“image”的成员

there is a few of them.

其中有几个。

after i added find_package(OpenCV REQUIRED to the CMakeLists.txt, i get this error

在我添加 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

what shld i do? I am using Apple and using Ubuntu 10.04.

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

Since i need

因为我需要

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

i added

我补充说

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

采纳答案by laksh

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

the include files above are included in the vision_opencv in ROS. so to include it, add the opencv dependency in the manifest file.

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

That would help.

那会有所帮助。

回答by Sergei Nikulov

  1. Get FindOpenCV.cmake from above link and put it anywhere on your computer.
  2. After cmake_minimum_required add line set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} [path to folder where you put FindOpenCV.cmake])
  3. Add find_package(OpenCV) to your CMakeLists.txt
  4. On this step you can check for OpenCV_FOUND and other OpenCV variables in your CMakeLists.txt
  1. 从上面的链接获取 FindOpenCV.cmake 并将其放在您计算机上的任何位置。
  2. 在 cmake_minimum_required 添加行集后(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} [放置 FindOpenCV.cmake 的文件夹的路径])
  3. 将 find_package(OpenCV) 添加到您的 CMakeLists.txt
  4. 在此步骤中,您可以检查 CMakeLists.txt 中的 OpenCV_FOUND 和其他 OpenCV 变量