fatal error: opencv2/opencv_modules.hpp: No such file or directory #include "opencv2/opencv_modules.hpp"

25,888

The solution is to just include_directories path till /usr/local/opencv4 and it works perfectly.

However, the best way I believe is to use the find_package function. I updated my Cmake to the following and it takes care of linking during build.

cmake_minimum_required(VERSION 3.15)
project(opencvTest)

set(CMAKE_CXX_STANDARD 17)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(opencvTest main.cpp)
target_link_libraries(opencvTest ${OpenCV_LIBS})
Share:
25,888

Related videos on Youtube

Atharva Dubey
Author by

Atharva Dubey

Updated on July 23, 2022

Comments

  • Atharva Dubey
    Atharva Dubey almost 2 years

    Hello all I am trying to use opencv-c++ API (version 4.4.0) which I have built from source. It is installed in /usr/local/ and I was simply trying to load and display an image using the following code -

    #include <iostream>
    #include <opencv4/opencv2/opencv.hpp>
    #include <opencv4/opencv2/core.hpp>
    #include <opencv4/opencv2/imgcodecs.hpp>
    #include <opencv4/opencv2/highgui.hpp>
    #include <opencv4/opencv2/core/cuda.hpp>
    
    using namespace cv;
    
    int main()
    {
        std::string image_path = "13.jpg";
        cv::Mat img = cv::imreadmulti(image_path, IMREAD_COLOR);
        if(img.empty())
        {
            std::cout<<"COULD NOT READ IMAGE"<<std::endl;
            return 1;
        }
        imshow("Display Window", img);
        return 0;
    }
    

    And when I compile it throws the following error during compilation -

    In file included from /CLionProjects/opencvTest/main.cpp:2:
    /usr/local/include/opencv4/opencv2/opencv.hpp:48:10: fatal error: opencv2/opencv_modules.hpp: No such file or directory
     #include "opencv2/opencv_modules.hpp"
    

    My Cmake is as follows -

    cmake_minimum_required(VERSION 3.15)
    project(opencvTest)
    
    set(CMAKE_CXX_STANDARD 17)
    include_directories("/usr/local/include/opencv4/opencv2/")
    add_executable(opencvTest main.cpp)
    target_link_libraries(opencvTest PUBLIC "/usr/local/lib/")
    

    I do not know what am I doing wrong here.. This might be a noob question, But I ahev just started using opencv in C++

    • drescherjm
      drescherjm over 3 years
      You need /usr/local/include/opencv4 to be in your include directories not /usr/local/include/opencv4/opencv2
    • Atharva Dubey
      Atharva Dubey over 3 years
      thanks a lot @drescherjm it works perfectly...
    • Owl
      Owl about 3 years
      What worked for me was: sudo cp -r /usr/local/include/opencv4/opencv2 /usr/include/
  • drescherjm
    drescherjm over 3 years
    This is indeed a better and more proper CMake solution.
  • Pawara Siriwardhane
    Pawara Siriwardhane about 3 years
    Please, use proper code formatting options available in StackOverflow editor when publishing an answer or a question.
  • Kris Stern
    Kris Stern over 2 years
    Thanks for this answer! It is exactly what I have been looking for for hours...

Related