2

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
0

2 Answers 2

4

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>)

Sign up to request clarification or add additional context in comments.

2 Comments

I chose a weird directory because I was at a computer at school and didn't have sudo privileges. At home now, on Ubuntu, going to try building OpenCV from source here. Do you have a link to a good page with installation instructions? I'm wondering in particular if there are dependencies I need to install before building OpenCV. I've found a number of installation instructions for OpenCV on the web so I'm not sure which one to pick. The official install instructions for Linux on opencv.org list some dependences but don't mention using ldconfig, which I think is necessary.
Here is a blog post that gives you instruction on how to install OpenCV in Ubuntu 14.04 with CUDA (GPU) support: blog.aicry.com/ubuntu-14-04-install-opencv-with-cuda
1

The PATH doesn't matter, you need to add the include path to the compiler include paths (the -I parameter of gcc). Or to the CPLUS_INCLUDE_PATH environment variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.