为什么CMake使用Opencv未正确设置Include目录(MSVC 2010项目)

时间:2014-05-03 13:53:45

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

我使用git查看了最新openCV版本的源代码,使用CMake创建了msvc解决方案文件,最后使用MSVC 2010构建了库。接下来我构建了Install项目以获取包含opencv / msvc_2010 / install目录子目录中的所有dll和头文件。我调整了PATH变量,将dll包含在opencv / msvc_2010 / install / bin中。到目前为止,上帝,但如果我现在尝试使用与以前相同的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时不会报告错误。 昨天我的旧版本一切正常并编译完成。有人看到这里有什么不对吗? btw QT被找到并正确包含在内。

1 个答案:

答案 0 :(得分:2)

用于查找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不会。