Search code examples
c++opencvincludeinclude-pathpath-variables

No such file or directory error when compiling C++ file with OpenCV headers


I'm on Red Hat Linux. I'm having some (probably newbie) problem with the includes in a C++ file. I created the following simple OpenCV script,

#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main(int argc, char ** argv){
    Mat img = imread( argv[1], -1 );
    if ( img.empty() ) return -1;
    namedWindow( "Example1", cv::WINDOW_AUTOSIZE );
    imshow( "Example1", img );
    waitKey( 0 );
    destroyWindow( "Example1" );
}

Then in the terminal I entered

g++ my_simple_script.cpp

and got the errors

newfile.cpp:1:39: error: opencv2/highgui/highgui.hpp: No such file or directory
newfile.cpp:3: error: 'cv' is not a namespace-name
newfile.cpp:3: error: expected namespace-name before ';' token
newfile.cpp: In function 'int main(int, char**)':
newfile.cpp:6: error: 'Mat' was not declared in this scope
newfile.cpp:6: error: expected ';' before 'img'
newfile.cpp:7: error: 'img' was not declared in this scope
newfile.cpp:8: error: 'cv' has not been declared
newfile.cpp:8: error: 'namedWindow' was not declared in this scope
newfile.cpp:9: error: 'img' was not declared in this scope
newfile.cpp:9: error: 'imshow' was not declared in this scope
newfile.cpp:10: error: 'waitKey' was not declared in this scope
newfile.cpp:11: error: 'destroyWindow' was not declared in this scope

I added

/home/m/maxwell9/2.4.3/include

to my PATH, where 2.4.3 indicates the version of OpenCV I'm using. When I type

echo $PATH

I see

/opt/apps/jdk1.6.0_22.x64/bin:/apps/smlnj/110.74/bin:/usr/local/cuda/bin:/sbin:/bin:/usr/sbin:/usr/bin:/apps/weka/3.7.12:/home/m/maxwell9/bin:/home/m/maxwell9/2.4.3/include

I confirmed that there is a file at

/home/m/maxwell9/2.4.3/include/opencv2/highgui/highgui.hpp

Solution

  • Just adding the include path will only resolve your compile problem. You will still see linker errors.. (and right way of adding include path is using -I flag, PATH is not used for this..)

    To compile and link your program successfully, you will need to both specify the Include path for header files and linker path to the pre-compiled OpenCV libraries and the list of libraries to be linked...

    1. The standard way, had you installed the openCV to the standard installation directory,by using the following sequence

       sudo make install (from your OpenCV build library)
       echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/opencv.conf
       sudo ldconfig
       printf '# OpenCV\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig\nexport PKG_CONFIG_PATH\n' >> ~/.bashrc  
       source ~/.bashrc  
      

    the following would have compiled and linked your program successfully for you :

    g++ my_simple_script.cpp `pkg-config --libs opencv` `pkg-config --cflags opencv`
    
    1. But apparently you have not done that.. as you are trying to point to a non-standard include path. Hence in your case you will need to specify your include path explicitly by using -I flag and your pre-compiled library path by -L flag and list out all the individual libraries you might want to use by using -l<name_of_library>

      g++ my_simple_script.cpp -I /home/m/maxwell9/2.4.3/include -L /home/m/maxwell9/2.4.3/<your build directory name>/lib/ -lopencv_core
      

    (list of other openCV libraries you may need will have to be appended to above command using format: -l<name of the lib you need>)