Compile and link OpenCV to my project in my main CMakeLists.txt

不想你离开。 提交于 2021-02-08 03:01:43

问题


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

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)

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)

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

回答1:


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.

Root CMakeLists.txt should have the following content:

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

add_subdirectory(node)

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
)

The CMakeLists.txt under third_party should contain only:

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

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

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.



来源:https://stackoverflow.com/questions/50422351/compile-and-link-opencv-to-my-project-in-my-main-cmakelists-txt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!