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

29,677

Solution 1

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

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

Solution 2

In case you're running CMake, you can also ask to

find_package (OpenCV 4.0.0 REQUIRED)
include_directories ("/usr/include/opencv4/")

Solution 3

Try compiling it with this:

FLAGS="$(pkg-config --cflags --libs opencv4)"
g++ cv.cpp -o cv $FLAGS
Share:
29,677
Notemaster
Author by

Notemaster

I'm a student in University at the moment. My interests include learning more about Programming Languages, Category Theory, and Webassembly. I like to play video games and do philosophy as well.

Updated on April 27, 2021

Comments

  • Notemaster
    Notemaster almost 3 years

    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?

  • Intey
    Intey over 2 years
    A little bit less hardcoded variant, would be include_directores(${OpenCV_INCLUDE_DIRS})
  • Patrizio Bertoni
    Patrizio Bertoni about 2 years
    You just have a typo in the word "directories"