2

I have install opencv in ubuntu 18.04 and it was installed successfully, I have tried this command: $ pkg-config --modversion opencv and its output is: 4.0.1-dev after this i have tried to rum c++ code:

#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;

int main( int argc, char** argv ) {

  cv::Mat image;
  image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);

  if(! image.data ) {
      std::cout <<  "Could not open or find the image" << std::endl ;
      return -1;
    }

  cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
  cv::imshow( "Display window", image );

  cv::waitKey(0);
  return 0;
}

with this command: :~/cpp_test$ g++ main.cpp -o output pkg-config --cflags --libs opencv but it throws a fatal error:

main.cpp:1:10: fatal error: opencv2/highgui.hpp: No such file or directory
 #include <opencv2/highgui.hpp>
          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.

I have reviewed some similar questions but i did not find my answer, i think this is because of environment variables and i do not know which variables i have to set.

danyialKhan
  • 697
  • 2
  • 8
  • 12
  • First find the `opencv2` folder in your include directories. Then see if the file `highgui.hpp` exists. – drescherjm Feb 27 '19 at 19:40
  • can you try include with full path? like #include – Rifat Alptekin Çetin Feb 27 '19 at 19:43
  • If '**highgui.hpp**' exist then i have to add path to '**bashrc**'?? – danyialKhan Feb 27 '19 at 19:44
  • I tried to include full path in **#include ** but now it throws another error: – danyialKhan Feb 27 '19 at 19:51
  • In file included from main.cpp:1:0: **/home/danyial/opencv/modules/highgui/include/opencv2/highgui.hpp:46:28: fatal error: opencv2/core.hpp: No such file or directory #include "opencv2/core.hpp"** ^ compilation terminated. – danyialKhan Feb 27 '19 at 19:52
  • 1
    You need to add `/home/danyial/opencv/modules/highgui/include/` to your compiler include paths. Related: https://stackoverflow.com/questions/12654013/how-to-make-g-search-for-header-files-in-a-specific-directory/12654056 – drescherjm Feb 27 '19 at 19:58

1 Answers1

3

In the compiling command add a "4" next to "opencv" (or the number of your version of OpenCV):

$ g++ main.cpp -o output \`pkg-config --cflags --libs opencv4\`
nj2237
  • 1,220
  • 3
  • 21
  • 25
Pedro
  • 31
  • 3