# Using opencv in linux

**URL:** <https://forum.opencv.org/t/using-opencv-in-linux/3376>\
**Category:** C++\
**Tags:** build, cmake\
**Created:** [May 17, 2021, 10:36pm UTC](https://forum.opencv.org/t/using-opencv-in-linux/3376 "2021-05-17T22:36:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivo94](https://avatars.discourse-cdn.com/v4/letter/i/51bf81/32.png) [@ivo94](https://forum.opencv.org/u/ivo94)\
**Post date:** [May 17, 2021, 10:36pm UTC](https://forum.opencv.org/t/using-opencv-in-linux/3376/1 "2021-05-17T22:36:39Z")

</div>

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](https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html) 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](https://docs.opencv.org/master/db/df5/tutorial_linux_gcc_cmake.html) 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:  
> 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.

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.

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [May 18, 2021, 8:09am UTC](https://forum.opencv.org/t/using-opencv-in-linux/3376/2 "2021-05-18T08:09:04Z")

</div>

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