Let me introduce my problem. I have a project and I’m using git to maintain it. I added opencv as submodule: git submodule add https://github.com/opencv/opencv.git external/opencv
In my top level cmake I added:
add_subdirectory(external/opencv)
find_package(OpenCV REQUIRED)
target_link_libraries (my_target ${OpenCV_LIBRARIES})
target_include_directories(my_target
PUBLIC ${OpenCV_INCLUDE_DIRS})
And run:
cmake -S . -B out/build
And I got error:
CMake Error at CMakeLists.txt:10 (find_package):
Could not find a package configuration file provided by “OpenCV” with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of “OpenCV” to CMAKE_PREFIX_PATH or set
“OpenCV_DIR” to a directory containing one of the above files. If “OpenCV”
provides a separate development package or SDK, be sure it has been
installed.
If I remove find_package from cmake, then cmake runs without problems, but when I try to build, then I get error of including opencv in main.cpp of my_target:
#include <opencv2/opencv.hpp>
I get error that it cannon find the header:
fatal error: opencv2/opencv.hpp: No such file or directory
3 | #include <opencv2/opencv.hpp>
Intelisense seems to have no problems at all, I can navigate into that header from the editor.
I’m using Ubuntu 22.
no idea, what you intended by using a git submodule, however, this is the src code only ! you need to build the opencv libs before, and point OpenCV_DIR to the install dir
I get it, but I need to configure cmake first and then run build, but it already complains at cmake configuration step. What I want to do is not to install opencv somewhere, but compile it in my project and then link it to my code that it needs it.
I commented out everythin except opencv in my cmake and I can configure and build it actually. I think my problem is most likely related to cmake build and link order than opencv itself…
EDIT: Ok, so I built opencv for sure and then added it to the project. This cmake configuration works:
add_subdirectory(external/opencv)
target_include_directories(my_target
PUBLIC ${OpenCV_INCLUDE_DIRS})
target_link_libraries (my_target ${OpenCV_LIBRARIES})
But in my_target I can’t include: #include <opencv2/opencv.hpp>
It complains:
main.cpp:3:10: fatal error: opencv2/opencv.hpp: No such file or directory
3 | #include <opencv2/opencv.hpp>
If I add this to cmake: find_package(OpenCV REQUIRED)
I got this cmake error from the original post. Cmake documentation doesn’t say anything about setting OpenCV_DIR, which path should I set to it?
you have to. e.g. the include folder gets assembled during install (it does not exist else)
you can still install to /home/frodo/ocv (CMAKE_INSTALL_PREFIX) or similar, point OpenCV_DIR at that later for your own prog, and delete it, ,when no more needed