1

I'm working on an academic research, implementing new search algorithms, based on Fast-Downward, a c++ open source that implement several Search Algorithms and many search domains and problems. Also installed Lab, a Python package that manage the compilation and the run scripting.

Recently I tried to include OpenCV library for its ML algorithms, in order to integrate those algorithms in my search algorithm.

But I encouraged some problems when including some libs in my FD project, and could not compile OpenCV with FD.

Project hierarchy:

fast_downward
| -- benchmarks
| -- experiments
| -- lab
| -- misc
| -- src
|    | -- preprocess
|    | -- search
|    | -- translate
|    | -- VAL
|    | -- ML

The ML directory is the new sub-directory I want to compile.

compilation attempt:

under src/CMakeLists.txt:

include(ML/CMakeLists.txt)

add_subdirectory(ML)

but the compilation failed.

To be honest, I've never tried to edit a CMake file, and I don't know the strategy and how to do that.

So, my question is How to integrate an OpenCV package/sub-folder (in my project called ML) in my main project?

EDIT:

Distributor ID: Ubuntu
Description:    Ubuntu 14.04 LTS
Release:        14.04
Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40

1 Answers1

2

You should not need include(ML/CMakeLists.txt), add_subdirectory(ML) should be enough inside src/CMakeLists.txt.
Now, we can generally have two cases:

  1. You have a system-wide installation of OpenCV (e.g. via apt-get, etc.), so you use the system version of OpenCV
  2. You include the sources of OpenCV inside the project. You build it and you use this version of OpenCV.

These two cases need to be dealt with in slightly different ways.

System wide OpenCV installation

Let's assume OpenCV is installed somewhere in your system. Then in src/ML/CMakeLists.txt you should have some lines like the following:

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(test ${YOUR_SOURCES} ${YOUR_HEADERS})
target_link_libraries(test ${OpenCV_LIBS})

this is to produce an executable, but if you want to produce a library, you should have add_library(...) instead of add_executable(...). If you need specific components from OpenCV you should ask for it with find_package(OpenCV REQUIRED COMPONENTS core imgproc ...) and of course consult the OpenCV doc. Mind that your CMake might ship without FindOpenCV.cmake script so you might not able to use find_package(OpenCV). In this case you need to grab one from the web. Also consult its source, as it might define different variables that hold the includes and libraries, which means that might happen that instead of using for example target_link_libraries(test ${OpenCV_LIBS}) you should use target_link_libraries(test ${OpenCV_LIBRARIES}).

OpenCV sources you downloaded and included inside/outside the package

In this case, I imagine you have the OpenCV package/sources in some directory in your system; mind in this case you need to build OpenCV, apt-get will not do the work for you. In your src/ML/CMakeLists.txt you'll have something like

ExternalProject_Add(my_opencv
   CMAKE_ARGS -D BUILD_SHARED_LIBS=NO ...
   CMAKE_INSTALL_PREFIX=${MY_OPENCV_INSTALLATION_DIR} 
   SOURCE_DIR ${PATH_TO_OPENCV_SRCS} 
)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(test ${YOUR_SOURCES} ${YOUR_HEADERS})
target_link_libraries(test ${OpenCV_LIBS})

Sources worth consulting are:
OpenCV intro: cmake
linking opencv libraries included as an external project via cmake
Configuring an c++ OpenCV project with Cmake
Could not find module FindOpenCV.cmake ( Error in configuration process)
CMake and OpenCV 3.0
sample FindOpenCV.cmake

Community
  • 1
  • 1
fedepad
  • 4,509
  • 1
  • 13
  • 27
  • the second part is my case. I have a project and inside it I putted the OpenCV sources. So as I understand from you answer is that if I want to compile all togather and be able to call OpenCV's class from another source file in my project, all I need to do is to put your example cmake code in OpenCV's CMakeLists.txt file? and when I compile my entire project I will compile all together? – Gal Dreiman Jan 08 '17 at 13:51
  • 1
    No, the 'sample' lines in CMakeLists.txt in the second case should not go in OpenCV's CMakeLists.txt but in your CMakeLists.txt. Basically you're telling in your CMakeLists.txt to produce the executable or library: "Build OpenCV (subtleties here and different cases...), Find OpenCV, use OpenCV's headers and libraries for my executable or library". – fedepad Jan 08 '17 at 14:01