在主CMakeLists.txt中编译OpenCV并将其链接到我的项目 [英] Compile and link OpenCV to my project in my main CMakeLists.txt

查看:118
本文介绍了在主CMakeLists.txt中编译OpenCV并将其链接到我的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to cmake. I have a project which uses dlib and opencv. They are defined as submodules which are in third_party folder. I want to link them to my main project which is 'node' with cmake but I could not achieved. I am sharing my project tree. I did with find_package(OpenCV) and target_link_libraries(recognition-node ${OPENCV_LIBS}) way but I need to compile from source without installing anything. At last, I just want to write 'cmake . && make'

.
├── 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的内容

Content of my top 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)

node/CMakeLists.txt的内容

Content of node/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'阶段给出错误,即:

It gives error in 'make' stage which is :

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

推荐答案

由于您坚持将opencv保留在项目树中

Since you insist on keeping the opencv in your project tree

这是一种更简单的方法,但我只希望它以这种方式进行.

It is easier way but I just want it to do in this way.

以下是肯定与您在问题中发布的项目树以及

Here is the solution that for sure works fine with your project tree that you posted in the question and with opencv-3.4.1. For simplicity I will neglect dlib library and Qt dependency, since you didn't have any problem with it.

CMakeLists.txt 应该具有以下内容:

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

add_subdirectory(node)

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

The CMakeLists.txt under the node directory should have the following content:

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
)

third_party 下的 CMakeLists.txt 仅应包含:

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

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

The sample I used to verify the build is contours2.cpp (just copy pasted the content into main.cpp).

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

However, I still think that it is a terrible idea to use this solution.

  • OpenCv确实要花费很多时间
  • 您必须手动添加include dirs(可以使用一些宏生成器,但通常看起来更难看)
  • 在构建系统中,您确实有很多不需要的目标(超过300个),包括 install target
  • OpenCv takes really a lot of time to compile
  • you have to manually add include dirs (you can use some macro generators, but usually it looks even more ugly)
  • in your build system you have a lot of targets (over 300) that you don't really need, including install target

因此,我的建议是:如果需要,可以将此解决方案用于科学目的,而只是在真正需要使用OpenCv时(如果不是管理员,则可以在系统范围内)编译并安装OpenCv.

So, my recommendation is: if you want, use this solution for scientific purpose, but just compile and install OpenCv system-wise (or locally, if you are not the admin) when you really need to use it.

这篇关于在主CMakeLists.txt中编译OpenCV并将其链接到我的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆