在我的主 CMakeLists.txt 中编译 OpenCV 并将其链接到我的项目

arn*_*nes 5 c++ opencv cmake

我是 cmake 的新手。我有一个使用 dlib 和 opencv 的项目。它们被定义为位于third_party文件夹中的子模块。我想将它们链接到我的主项目,即带有 cmake 的“节点”,但我无法实现。我正在分享我的项目树。我使用 find_package(OpenCV) 和 target_link_libraries(recognition-node ${OPENCV_LIBS}) 方式进行操作,但我需要从源代码进行编译而不安装任何东西。最后,我只想写 'cmake 。&& 制作'

\n\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 node\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build.sh\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 configure.sh\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 findfacestask.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 findfacestask.h\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 matrixwrapper.h\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 poolcontext.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 poolcontext.h\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 recognition.dat\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 recognizefacetask.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 recognizefacetask.h\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 runscript\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 sp.dat\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 task.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 task.h\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 unhandledexception.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 unhandledexception.h\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 webcamfeed.cpp\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 webcamfeed.h\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 wrapper.cpp\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wrapper.h\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 third_party\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dlib\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 appveyor.yml\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dlib\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docs\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 examples\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MANIFEST.in\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 python_examples\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.py\n    \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tools\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 opencv\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 3rdparty\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 apps\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 cmake\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CONTRIBUTING.md\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 doc\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 include\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 LICENSE\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 modules\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 platforms\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 samples\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的顶级 CMakeLists.txt 的内容

\n\n
cmake_minimum_required(VERSION 2.8.12)\n\nset (CMAKE_CXX_STANDARD 11)\n\nadd_subdirectory(node)\nadd_subdirectory(third_party/dlib)\nadd_subdirectory(third_party/opencv)\n
Run Code Online (Sandbox Code Playgroud)\n\n

节点/CMakeLists.txt的内容

\n\n
cmake_minimum_required(VERSION 2.8.12)\nproject(recognition-node)\n\nset(CMAKE_AUTOMOC ON)\n\nfind_package(Qt5Widgets REQUIRED)\n\nadd_executable(recognition-node main.cpp  \n            webcamfeed.cpp \n            poolcontext.cpp \n            unhandledexception.cpp\n            task.cpp\n            findfacestask.cpp\n            wrapper.cpp\n            recognizefacetask.cpp)\n\ntarget_link_libraries(recognition-node Qt5::Widgets)\ntarget_link_libraries(recognition-node dlib::dlib)\ntarget_link_libraries(recognition-node opencv::core)\n
Run Code Online (Sandbox Code Playgroud)\n\n

它在“make”阶段给出错误:

\n\n
/home/arnes/workspace/recognition-node/node/poolcontext.h:10:28: fatal error: \nopencv2/core.hpp: No such file or directory\n
Run Code Online (Sandbox Code Playgroud)\n

Pta*_*666 2

既然你坚持将 opencv 保留在你的项目树中

这是更简单的方法,但我只想以这种方式进行。

这是肯定可以与您在问题中发布的项目树以及opencv-3.4.1配合使用的解决方案。为了简单起见,我将忽略dlib库和Qt依赖项,因为您对它没有任何问题。

根目录CMakeLists.txt应包含以下内容:

cmake_minimum_required(VERSION 2.8.11) # or anything higher, if you wish
project(recognition-node CXX)

add_subdirectory(node)
Run Code Online (Sandbox Code Playgroud)

目录CMakeLists.txtnode应有以下内容:

add_subdirectory(third_party)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g") # or any other additional flags

# at this point you can add find_package(Qt5Widgets REQUIRED) and later link your binary against Qt5::widgets as well
add_executable(myExec main.cpp
# and put here all the other source files of your project ...
)
# for linking libs I have put additionally highgui and imgproc to check the solution against OpenCV official sample
target_link_libraries(myExec opencv_core opencv_highgui opencv_imgproc)

target_include_directories(myExec PUBLIC 
    third_party/opencv/modules/calib3d/include
    third_party/opencv/modules/core/include
    third_party/opencv/modules/cudaarithm/include
    third_party/opencv/modules/cudabgsegm/include
    third_party/opencv/modules/cudacodec/include
    third_party/opencv/modules/cudafeatures2d/include
    third_party/opencv/modules/cudafilters/include
    third_party/opencv/modules/cudaimgproc/include
    third_party/opencv/modules/cudalegacy/include
    third_party/opencv/modules/cudaobjdetect/include
    third_party/opencv/modules/cudaoptflow/include
    third_party/opencv/modules/cudastereo/include
    third_party/opencv/modules/cudawarping/include
    third_party/opencv/modules/cudev/include
    third_party/opencv/modules/dnn/include
    third_party/opencv/modules/features2d/include
    third_party/opencv/modules/flann/include
    third_party/opencv/modules/highgui/include
    third_party/opencv/modules/imgcodecs/include
    third_party/opencv/modules/imgproc/include
    third_party/opencv/modules/ml/include
    third_party/opencv/modules/objdetect/include
    third_party/opencv/modules/photo/include
    third_party/opencv/modules/shape/include
    third_party/opencv/modules/stitching/include
    third_party/opencv/modules/superres/include
    third_party/opencv/modules/ts/include
    third_party/opencv/modules/video/include
    third_party/opencv/modules/videoio/include
    third_party/opencv/modules/videostab/include
    third_party/opencv/modules/viz/include
    third_party/opencv/modules/world/include
)
Run Code Online (Sandbox Code Playgroud)

下面应该只包含CMakeLists.txtthird_party

add_subdirectory(opencv)
# add_subdirectory(dlib) # if you will use dlib, of course also add dlib
Run Code Online (Sandbox Code Playgroud)

我用来验证构建的示例是contours2.cpp(只需将内容复制粘贴到main.cpp)。

然而,我仍然认为使用这个解决方案是一个糟糕的主意。

  • OpenCv 确实需要很多时间来编译
  • 你必须手动添加包含目录(你可以使用一些宏生成器,但通常它看起来更难看)
  • 在你的构建系统中,你有很多你并不真正需要的目标(超过 300 个),包括installtarget

所以,我的建议是:如果你愿意,可以将此解决方案用于科学目的,但当你真正需要使用它时,只需在系统上(或在本地,如果你不是管理员)编译和安装 OpenCv 即可。

  • 有很多充分的理由手动编译 OpenCV 并将其保存在项目树中。移动应用程序或嵌入式设备将需要具有特定选项集的交叉编译版本。 (6认同)