2

I have an OpenCV installation on Ubuntu Linux. From any example the headers are:

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

When I compile with these includes I get the error :

fatal error: opencv2/imgcodecs.hpp: No such file or directory

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>

I can find out where OpenCV is located with

pkg-config --cflags --libs opencv

which outputs as follow:

 "Package opencv was not found"

When I try pkg-config --cflags --libs opencv2

I get:

"Package opencv2 was not found"

Only when I use pkg-config --cflags --libs opencv4 do I get the path:

-I/usr/local/include/opencv4/opencv2 

so I thought that by changing the includes to:

#include <opencv4/imgcodecs.hpp>
#include <opencv4/highgui.hpp>

it would work. But I get:

fatal error: opencv4/imgcodecs.hpp: No such file or directory

#include <opencv4/imgcodecs.hpp>

so when I try the full path:

#include <opencv4/opencv2/imgcodecs.hpp>

I get:

imgcodecs.hpp:46:10: fatal error: opencv2/core.hpp: No such file or directory

#include "opencv2/core.hpp"

and of course in the hpp file in question it is:

#include "opencv2/core.hpp"

Is there a simple way to fix all this?

3
  • The simplest way is to use a package manager like Conan Commented May 28, 2020 at 16:12
  • i am using a makefile and tried with Cmake with the same outcomes , now the crazy part is i have a make file on another piece of code not written by me that works with #ifdef , i can share that as well Commented May 28, 2020 at 16:20
  • I prefer a package manager for development. It's difficult to manage the versions with system packages. Conan downloads the exact version and all its dependencies and sets all necessary paths. If you want it simple use Conan with CMake. Commented May 28, 2020 at 17:39

1 Answer 1

3

I actually ran into this issue recently. The issue is that when you build OpenCV it places the header files in /usr/local/include/opencv4/ then underneath that you will see opencv2/opencv.hpp and all the other files you're referencing. To fix your issue you will need to run in bash:

sudo ln -s /usr/local/include/opencv4/opencv2 /usr/local/include/opencv2

This resolved the issue for me, if it works for you please be sure to mark this as the answer.

Sign up to request clarification or add additional context in comments.

2 Comments

This should be the correct answer. This can also be fixed by changing the build settings of CMake before building openCV from source.
Solved the issue for me too. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.