# CMake – problem with adding opencv

**URL:** <https://forum.opencv.org/t/cmake-problem-with-adding-opencv/10271>\
**Category:** C++\
**Tags:** build, git\
**Created:** [September 21, 2022, 10:55am UTC](https://forum.opencv.org/t/cmake-problem-with-adding-opencv/10271 "2022-09-21T10:55:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![serwus](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@serwus](https://forum.opencv.org/u/serwus)\
**Post date:** [September 21, 2022, 10:55am UTC](https://forum.opencv.org/t/cmake-problem-with-adding-opencv/10271/1 "2022-09-21T10:55:33Z")

</div>

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:

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

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

```auto
#include <opencv2/opencv.hpp>

```

I get error that it cannon find the header:

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 21, 2022, 11:45am UTC](https://forum.opencv.org/t/cmake-problem-with-adding-opencv/10271/2 "2022-09-21T11:45:23Z")

</div>

> [@serwus](#):
>
> git submodule add [GitHub - opencv/opencv: Open Source Computer Vision Library](https://github.com/opencv/opencv.git) external/opencv

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

---

<div class="post-metadata">

**Author:** ![serwus](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@serwus](https://forum.opencv.org/u/serwus)\
**Post date:** [September 21, 2022, 6:11pm UTC](https://forum.opencv.org/t/cmake-problem-with-adding-opencv/10271/4 "2022-09-21T18:11:24Z")

</div>

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:

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

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

---

<div class="post-metadata">

**Author:** ![serwus](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@serwus](https://forum.opencv.org/u/serwus)\
**Post date:** [September 24, 2022, 10:09am UTC](https://forum.opencv.org/t/cmake-problem-with-adding-opencv/10271/5 "2022-09-24T10:09:10Z")

</div>

I tried exactly the same steps with different library (glfw) and it works perfectly fine.

In terminal:  
`git submodule add <link> external/glfw`

Added to cmake:

```auto
add_subdirectory(external/glfw)
target_include_directories(my_target
    PUBLIC external/glfw/include)
target_link_libraries(my_target
    glfw)

```

In terminal once again:

```auto
cmake -S . -B out/build
cd out/build ; ./my_target/my_target

```

But it doesn’t work for opencv…

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 24, 2022, 10:23am UTC](https://forum.opencv.org/t/cmake-problem-with-adding-opencv/10271/6 "2022-09-24T10:23:45Z")

</div>

> [@serwus](#):
>
> What I want to do is not to install opencv somewhere

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

> [@serwus](#):
>
> I tried exactly the same steps with different library (glfw)

useless / misleading
