gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:51 26 4
gpt4 key购买 nike

我是 cmake 的新手。我有一个使用 dlib 和 opencv 的项目。它们被定义为位于 third_party 文件夹中的子模块。我想将它们链接到我的主要项目,即带有 cmake 的“节点”,但我无法实现。我正在分享我的项目树。我用 find_package(OpenCV) 和 target_link_libraries(recognition-node ${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

最佳答案

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

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

这是肯定适用于您在问题中发布的项目树和 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)

node目录下的CMakeLists.txt应该有如下内容:

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 中)。

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

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

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

关于c++ - 在我的主 CMakeLists.txt 中编译 OpenCV 并将其链接到我的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50422351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com