Using opencv in linux

Hello, I am just getting started with opencv, and I know that this has probably been asked plenty of times, but so far I haven’t been able to successfully compile any source file that uses opencv libraries. I followed this tutorial. Specifically, I’ve run the commands from the Build with opencv_contrib section of the aforementioned tutorial.

After this, I tried running the example given here just as the tutorial indicates, but got the following error:

CMake Error at CMakeLists.txt:3 (find_package):
By not providing “FindOpenCV.cmake” in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by “OpenCV”, but
CMake did not find one.
Could not find a package configuration file provided by “OpenCV” with any
of the following names:
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

Obviously, I am not quite sure what I am doing or what should I do in order to fix it.
Finally, and out of curiosity, if I wanted to run the example without using cmake, how could this be done? As far as I know, I should be using the -I and -L flags so that the compiler is able to find the headers and the libraries, but I’m not 100% sure how this is done either.
Thank you in advance for your help.

Well, the error message says it all. It doesn’t find the OpenCVConfig.cmake file, so you have to set the path manually. Add:

set(OpenCV_DIR "/usr/local/lib/cmake/opencv4")

(or whatever your path is) to the CMakeLists.txt file.

To build your app manually, it’s a bit more complicated, as there are a dozen lib files to link your application to. If you have OpenCV built with pkg-config enabled, it’s easier:

gcc mycode.cpp -o mycode `pkg-config opencv4 --cflags` `pkg-config opencv4 --libs`