Opencv 子目录中的CMake文件无法访问外部库

Opencv 子目录中的CMake文件无法访问外部库,opencv,cmake,Opencv,Cmake,我的项目结构如下所示: Project | Dependency functions.cpp functions.h | DisplayImage.cpp 我想在functions.cpp中使用opencv。当我在DisplayImage中使用opencv函数时,它可以工作,但当我在functions.h中包含opencv2/imgproc.hpp时,我会得到一个“文件未找到错误” 我的CMakeLists在根目录中如下所示: cmake_minimum_req

我的项目结构如下所示:

Project
  | Dependency
      functions.cpp
      functions.h
  | DisplayImage.cpp
我想在functions.cpp中使用opencv。当我在DisplayImage中使用opencv函数时,它可以工作,但当我在functions.h中包含opencv2/imgproc.hpp时,我会得到一个“文件未找到错误”

我的CMakeLists在根目录中如下所示:

cmake_minimum_required(VERSION 3.10)

project(DisplayImage)

set("OpenCV_DIR" "~/opencv/build")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenCV REQUIRED)

add_subdirectory(Dependency)

add_executable(DisplayImage DisplayImage.cpp)

target_link_libraries(DisplayImage ${OpenCV_LIBS})

include_directories("${CMAKE_SOURCE_DIR}/Dependency")
include_directories("~/CppLibraries/eigen-3.3.7/Eigen")
依赖目录中的CMakeLists.txt文件包含:

add_library(Dependency functions.cpp functions.h)
为什么在functions.h中找不到opencv2/imgproc.hpp

这是我收到的错误消息:

$ cmake --build .
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bn/Documents/C++/opencv_playground/build
Scanning dependencies of target DisplayImage
[ 25%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[ 50%] Linking CXX executable DisplayImage
[ 50%] Built target DisplayImage
[ 75%] Building CXX object Dependency/CMakeFiles/Dependency.dir/functions.cpp.o
In file included from /Users/bn/Documents/C++/opencv_playground/Dependency/functions.cpp:1:
/Users/bn/Documents/C++/opencv_playground/Dependency/functions.h:1:10: fatal error: 'opencv2/imgproc.hpp'
      file not found
#include <opencv2/imgproc.hpp>
         ^~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [Dependency/CMakeFiles/Dependency.dir/functions.cpp.o] Error 1
make[1]: *** [Dependency/CMakeFiles/Dependency.dir/all] Error 2
make: *** [all] Error 2
$cmake——构建。
--配置完成
--生成完成
--构建文件已写入:/Users/bn/Documents/C++/opencv\u/Build
目标显示图像的扫描相关性
[25%]构建CXX对象CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[50%]链接CXX可执行文件DisplayImage
[50%]内置目标显示图像
[75%]构建CXX对象依赖项/cmakfiles/Dependency.dir/functions.cpp.o
在/Users/bn/Documents/C++/opencv_/Dependency/functions包含的文件中。cpp:1:
/Users/bn/Documents/C++/opencv_/Dependency/functions.h:1:10:致命错误:“opencv2/imgproc.hpp”
找不到文件
#包括
^~~~~~~~~~~~~~~~~~~~~
生成1个错误。
make[2]:***[Dependency/CMakeFiles/Dependency.dir/functions.cpp.o]错误1
生成[1]:***[Dependency/CMakeFiles/Dependency.dir/all]错误2
make:**[全部]错误2

在依赖项子目录中的CMakeLists.txt文件中,我需要包含目标链接库(Dependency PUBLIC${OpenCV_LIBS}),因此CMakeLists文件如下所示:

add_library(Dependency functions.cpp functions.h)
target_link_libraries(Dependency PUBLIC ${OpenCV_LIBS})

出于调试目的,您可以在“find_package(需要OpenCV)”之后的CMakeLists.txt中添加以下行


当您在终端中执行“cmake”时,它将打印出所有opencv库并包括路径。

请将完整的错误消息复制/粘贴到您的问题帖子中。此外,使用<代码> ~路径快捷方式已知会导致CMake出现问题,因此请考虑写出完整路径。此外,您还没有将OpenCV标题目录添加到CGASE中的包含目录(使用<代码> OpenCVI-包含DIRS < /代码>),所以我惊讶地发现任何OpenCV标题都有。请参阅OpenCV说明,有一个非常类似的问题得到了回答。感谢您的评论。我从源代码构建了opencv,我必须在include_directories命令中列出哪个目录?build/include目录中没有任何头文件。因为您使用了
find_package(OpenCV…
),如果确实正确找到了OpenCV,则应填充
OpenCV_include_DIRS
变量。因此
include_目录(${OpenCV_include_DIRS})
应足以将OpenCV头添加到项目中。
message(STATUS "cv libs: ${OpenCV_LIBS}")
message(STATUS "include_path: ${OpenCV_INCLUDE_DIRS}")