c++ - 为什么 CMake 使用 Opencv 未正确设置包含目录(MSVC 2010 项目)

标签 c++ qt opencv cmake visual-studio-2010

我使用 git 检查了最新 openCV 版本的源代码,使用 CMake 创建了 msvc 解决方案文件,最后使用 MSVC 2010 构建了库。接下来,我构建了安装项目以获取 opencv/msvc_2010/install 目录,其中包含子目录中的所有 dll 和头文件。我调整了 PATH 变量以包含 opencv/msvc_2010/install/bin 中的 dll。到目前为止,上帝,但如果我现在尝试使用与以前相同的 CMakeLists 文件来编译我的旧项目,CMake 不会在项目属性中设置 opencv 头文件,并且我会收到以下错误:

fatal error C1083: Cannot open include file: 'opencv2\opencv.hpp': No such file or directory

我的 CMakeLists.txt 文件通常如下所示:

#CMakeLists.txt for Qt5 projects
#
# Main changes to Qt4: target_link_libraries, include_directories,
# and add_definitions is not needed anymore and replaced by
# qt5_use_modules.
# QT5_WRAP_CPP is replaced by set(CMAKE_AUTOMOC ON).

cmake_minimum_required(VERSION 2.8.9)

#Set project name
set(PROJECT_NAME ImageClient)
project(${PROJECT_NAME})

#Search for Qt5
find_package(Qt5Widgets REQUIRED)
#Search for OpenCV
find_package(OpenCV REQUIRED)

# Assure that moc is run when needed. Removing this line, you will
# have to run moc manually or get linker errors.
set(CMAKE_AUTOMOC ON)
set(QT_USE_QTNETWORK TRUE)

# This line includes the current Dir of CMake. The xxx.ui file is
# converted to a ui_xxx.h file by uic.exe. The header ui_xxx.h file
# is located in the build directory. To make this header visible to
# MSVS the build path needs to be included.
set(CMAKE_INCLUDE_CURRENT_DIR ON)

#Set sources and headers and forms
set(SOURCES src/Main.cpp src/CImageClient.cpp src/CCameraHandler.cpp
src/CCameraCapture.cpp src/CImageWidget.cpp src/CSnapAndFind.cpp)
set(HEADERS src/CImageClient.h src/CCameraHandler.h
src/CCameraCapture.h src/CImageWidget.h src/CSnapAndFind.h)
set(FORMS src/snapAndFind.ui)

#set(RESOURCES src/xxx.rc)

#This line is necessary to cal uic.exe and creates the ui_xxx.h
#files from the xxx.ui files. The header is created the first time
#the project is compiled
QT5_WRAP_UI(FORMS_HEADERS ${FORMS})

#Add all source files to the executable
add_executable(${PROJECT_NAME} WIN32 ${SOURCES} ${HEADERS} ${FORMS_HEADERS} ${HEADERS_MOC})

target_link_libraries(${PROJECT_NAME} ${Qt5Core_QTMAIN_LIBRARIES}             ${Qt5_QTNETWORK_LIBRARY} ${OpenCV_LIBS})

#Qt5 macro encapsulates all of the variable usage required to use a
#Qt module.
qt5_use_modules(${PROJECT_NAME} Widgets Network)

当然,我可以手动设置附加包含目录,但我更喜欢由 CMake 构建的工作解决方案。 OpenCV_DIR 由 CMake 自动设置为 C:/clibs/opencv/build_msvs2010,这是正确的。但不知何故,它无法将头文件添加到项目中。运行CMake时没有报错。 昨天,我的旧版本一切正常并已编译。有人看出这里出了什么问题吗?顺便说一句,QT 已找到并正确包含。<​​/p>

最佳答案

用于查找 OpenCV 的 .cmake 文件以以下几行开头:

#
#  Usage from an external project:
#    In your CMakeLists.txt, add these lines:
#
#    FIND_PACKAGE(OpenCV REQUIRED)
#    TARGET_LINK_LIBRARIES(MY_TARGET_NAME ${OpenCV_LIBS})
#
#    Or you can search for specific OpenCV modules:
#
#    FIND_PACKAGE(OpenCV REQUIRED core highgui)
#
#    If the module is found then OPENCV_<MODULE>_FOUND is set to TRUE.
#
#    This file will define the following variables:
#      - OpenCV_LIBS                     : The list of libraries to links against.
#      - OpenCV_LIB_DIR                  : The directory(es) where lib files are. Calling LINK_DIRECTORIES
#                                          with this path is NOT needed.
#      - OpenCV_INCLUDE_DIRS             : The OpenCV include directories.
#      - OpenCV_COMPUTE_CAPABILITIES     : The version of compute capability
#      - OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API
#      - OpenCV_VERSION                  : The version of this OpenCV build. Example: "2.4.0"
#      - OpenCV_VERSION_MAJOR            : Major version part of OpenCV_VERSION. Example: "2"
#      - OpenCV_VERSION_MINOR            : Minor version part of OpenCV_VERSION. Example: "4"
#      - OpenCV_VERSION_PATCH            : Patch version part of OpenCV_VERSION. Example: "0"
#
#    Advanced variables:
#      - OpenCV_SHARED
#      - OpenCV_CONFIG_PATH
#      - OpenCV_LIB_COMPONENTS
#

这意味着您应该将此行添加到您的CMakeLists.txt中:

include_directories(${OpenCV_INCLUDE_DIRS})

Qt 会自动为您完成此操作,但 OpenCV 不会。

关于c++ - 为什么 CMake 使用 Opencv 未正确设置包含目录(MSVC 2010 项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23445327/

相关文章:

c++ - 如何防止字符串在 PROGMEM 中多次发出蜂鸣声

c++ - 自定义按钮形状

c++ - 显式实例化类模板中的自动构造函数

c++ - g++ -M -MF 选项输出一个目标文件,如何输出到所有目标文件

c++ - OpenCV 无法在自定义目录中编译

c++ - 在C++中初始化2D数组类并将其传递给函数

c++ - Dialog 关闭后嵌入式 QGraphicsView 不隐藏

c++ - 显示以每个方 block 为对象的棋盘 - C++ QT

c++ - 用mingw编译opencv3.1

c++ - 键盘输入以保存从opencv中的视频流中检测到的图像