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

时间:2018-05-19 06:10:23

标签: c++ opencv cmake

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

.
├── CMakeLists.txt
├── node
│   ├── build.sh
│   ├── CMakeLists.txt
│   ├── configure.sh
│   ├── findfacestask.cpp
│   ├── findfacestask.h
│   ├── main.cpp
│   ├── matrixwrapper.h
│   ├── poolcontext.cpp
│   ├── poolcontext.h
│   ├── recognition.dat
│   ├── recognizefacetask.cpp
│   ├── recognizefacetask.h
│   ├── runscript
│   ├── sp.dat
│   ├── task.cpp
│   ├── task.h
│   ├── unhandledexception.cpp
│   ├── unhandledexception.h
│   ├── webcamfeed.cpp
│   ├── webcamfeed.h
│   ├── wrapper.cpp
│   └── wrapper.h
└── third_party
    ├── dlib
    │   ├── appveyor.yml
    │   ├── CMakeLists.txt
    │   ├── dlib
    │   ├── docs
    │   ├── examples
    │   ├── MANIFEST.in
    │   ├── python_examples
    │   ├── README.md
    │   ├── setup.py
    │   └── tools
    └── opencv
        ├── 3rdparty
        ├── apps
        ├── cmake
        ├── CMakeLists.txt
        ├── CONTRIBUTING.md
        ├── data
        ├── doc
        ├── include
        ├── LICENSE
        ├── modules
        ├── platforms
        ├── README.md
        └── samples

我的顶级CMakeLists.txt的内容

cmake_minimum_required(VERSION 2.8.12)

set (CMAKE_CXX_STANDARD 11)

add_subdirectory(node)
add_subdirectory(third_party/dlib)
add_subdirectory(third_party/opencv)

节点/ CMakeLists.txt的内容

cmake_minimum_required(VERSION 2.8.12)
project(recognition-node)

set(CMAKE_AUTOMOC ON)

find_package(Qt5Widgets REQUIRED)

add_executable(recognition-node main.cpp  
            webcamfeed.cpp 
            poolcontext.cpp 
            unhandledexception.cpp
            task.cpp
            findfacestask.cpp
            wrapper.cpp
            recognizefacetask.cpp)

target_link_libraries(recognition-node Qt5::Widgets)
target_link_libraries(recognition-node dlib::dlib)
target_link_libraries(recognition-node opencv::core)

它在'make'阶段给出了错误:

/home/arnes/workspace/recognition-node/node/poolcontext.h:10:28: fatal error: 
opencv2/core.hpp: No such file or directory

1 个答案:

答案 0 :(得分:1)

因为你坚持在项目树中保留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)

CMakeLists.txt目录下的node应具有以下内容:

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
)

CMakeLists.txt下的third_party应仅包含:

add_subdirectory(opencv)
# add_subdirectory(dlib) # if you will use dlib, of course also add dlib

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

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

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

所以,我的建议是:如果你想使用该解决方案用于科学目的,但只是编译并安装OpenCV制度上(或本地,如果你不是管理员),当你真正需要使用它