Question

I checked out the sources of the most recent openCV version using git, created the msvc solution file using CMake and finally build the library with MSVC 2010. Next I build the Install project to obtain the opencv/msvc_2010/install directory which contains all the dlls and header files in subdirectories. I adjusted the PATH variable to include the dlls in opencv/msvc_2010/install/bin. So far so god, but if I now try to compile my old projects using the same CMakeLists files as before, no opencv header files are set in the project properties by CMake and I obtain the following error:

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

My CMakeLists.txt files usually look like this:

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

Of course I could manually set the Additionally Include directories but I would prefer a working solution build by CMake. The OpenCV_DIR is automatically set by CMake to C:/clibs/opencv/build_msvs2010 which is correct. But somehow it fails to add the header files to the project. No error is reported when CMake is run. Yesterday with my old build everything worked and compiled. Does anyone see whats wrong here? btw QT is found and included correctly.

Was it helpful?

Solution

The .cmake file used to find OpenCV starts with this lines:

#
#  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
#

It means that you should this line to your CMakeLists.txt:

include_directories(${OpenCV_INCLUDE_DIRS})

Qt does it automatically for you, but OpenCV doesn't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top