skip to Main Content

I’m trying to build one repo that has dependencies on OpenCV. In the CMakeLists.txt, I can see the following:

find_package(OpenCV 3.0 QUIET)
if(NOT OpenCV_FOUND)
   find_package(OpenCV 2.4.3 QUIET)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
   endif()
endif()

# ...

target_link_libraries(${PROJECT_NAME}
   ${OpenCV_LIBS}
   ${EIGEN3_LIBS}
   # ...
)

If I’m not wrong, this should have worked without any issues if the OpenCV had been installed in the /usr/local/opencv directory. However, the problem is OpenCV on my Linux machine (Ubuntu 18.04) is located at some custom location e.g. /usr/local/my_opencv/.

Most probably because of this, when I try to build the repo, I’m getting an error:

CMake Error at CMakeLists.txt:<line number> (message):
  OpenCV > 2.4.3 not found.

What changes do I need to make in order to get rid of this error and be able to build the repo?

This is what the directory structure of my OpenCV looks like:

milan@my_machine:/usr/local/my_opencv$ tree -L 5
.
├── bin
│   ├── opencv_annotation
│   └── ...
├── include
│   └── opencv4
│       └── opencv2
│           ├── ...
│           ├── core
│           │   ├── ...
│           │   ├── core_c.h
│           │   ├── core.hpp
│           │   └── ...
│           ├── core.hpp
│           ├── ...
│           └── ...
├── lib
│   ├── cmake
│   │   └── opencv4
│   │       ├── OpenCVConfig.cmake
│   │       └── ...
│   ├── ...
│   ├── libopencv_core.so -> libopencv_core.so.4.2
│   ├── libopencv_core.so.4.2 -> libopencv_core.so.4.2.0
│   ├── libopencv_core.so.4.2.0
│   ├── ...
│   ├── opencv4
│   │   └── 3rdparty
│   │       ├── ...
│   │       └── ...
│   ├── ...
│   └── python3.6
│       └── dist-packages
│           └── cv2
└── share
    ├── licenses
    │   └── opencv4
    │       ├── ...
    │       └── ...
    └── opencv4
        ├── ...
        │   └── ...
        ├── ...
        └── ...

2

Answers


  1. You can try to set OpenCV_DIR before to call find_package.

    set(OpenCV_DIR /path/to/opencv)
    
    Login or Signup to reply.
  2. You might be able to achieve this without modifying CMakeLists.txt files

    (and this might even be preferred, since you’ve specified that this has to do with idiosyncrasies of your local machine’s setup)

    If the package uses the Config mode of find_package, then you may be able to use <PackageName>_ROOT (variable or env variable), CMAKE_PREFIX_PATH , or set a <PackageName>_DIR variable. See the "Config Mode Search Procedure" docs steps #1, #2, and #3 respectively and the table at the top of that section on what subdirectories are searched. In your case, try setting the value of those variables to /usr/local/my_opencv.

    Variables can be set in CMakeLists.txt files with set(), or as cache variable on the commandline with -D. You can put this info into a CMakeUserPresets.json file to ease running repeated/multiple configurations.

    Otherwise (Module mode or if you insist)

    You can either take those same above approaches and just set/append-to those variables in the CMakeLists.txt files instead of initializing them via commandline, or if the package uses Config mode, you can also consider using the PATHS parameter.

    If the package uses Module mode, there are much fewer options. You could probably write a dependency provider to do this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search