How to fix "fatal error: opencv2/core.hpp: No such file or directory" for opencv4 installed in manjaro

Notemaster picture Notemaster · Oct 21, 2019 · Viewed 14.6k times · Source

Essentially, I've been able to install openCV fine for python but I also want to be able to do it for C++. I was able to install it using my linux distro's package manager (pacman for manjaro which is based on arch) but I haven't gotten the following program to work to test openCV

#include <iostream>
#include <opencv2/core.hpp>

int main() {
   std::cout << "OpenCV version: " << CV_VERSION << std::endl;
   return 0;
}

The error I get from trying to compile this program is the following:

cv.cpp:2:10: fatal error: opencv2/core.hpp: No such file or directory
    2 | #include <opencv2/core.hpp>
      |          ^~~~~~~~~~~~~~~~~~
 compilation terminated.

Apparently, this is a common error, however I have tried many solutions and they all seem to not work. I know where core.hpp is and it is actually at /usr/local/include/opencv4/opencv2/core.hpp. However, when I change the include statement to

 #include opencv4/opencv2/core.hpp

I get the following error

 In file included from cv.cpp:2:
 /usr/include/opencv4/opencv2/core.hpp:52:10: fatal error: opencv2/core                /cvdef.h: No such file or directory
   52 | #include "opencv2/core/cvdef.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~
 compilation terminated.

Indicating that all the header files path directories are wrong essentially. I tried to fix this with the following symbolic link but that didn't work either:

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

I also tried compiling in the following way:

$ g++ -o cv cv.cpp -I/usr/local/include/opencv4 -Lusr/local/lib -lopencv_core

That also didn't work and gave me the same errors. For reference, this is how I compiled before:

$ g++ cv.cpp -o cv

I also don't know if this means anything, but

$ pkg-config --libs opencv4

gives me the following results:

-lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm     -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core 

Although I have no idea what this means nor do I know if this helps me at all. I'd rather not have any other indirect solutions. Any ideas on how to fix this?

Answer

Notemaster picture Notemaster · Oct 21, 2019

Sorry, guys. Found my own answer. For me, this was the correct way to compile:

  g++ cv.cpp -o cv -I/usr/include/opencv4