gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:21:53 25 4
gpt4 key购买 nike

我的项目结构如下:

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

并且Dependency目录中的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

最佳答案

为了调试目的,您可以在“find_package(OpenCV REQUIRED)”之后的CMakeLists.txt中添加以下行。

message(STATUS "cv libs: ${OpenCV_LIBS}")
message(STATUS "include_path: ${OpenCV_INCLUDE_DIRS}")
当您执行“cmake。”时,在终端中,它将打印出所有opencv库并包含路径。

关于opencv - 子目录中的CMake文件无法访问外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61818072/

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