OpenCV2 and g++: running your first program

Here’s a sample ‘helloworld’ program which takes an image and displays it:

#include "opencv2/opencv.hpp"

using namespace cv;

int main( int argc, char** argv )
// Read the source file
Mat src;
src = imread("fruits.jpg");

// Show the results
imshow( " ORIGINAL ", src );

return 0;

Save it is ‘helloworld.cpp’ using your favorite editor. Download a ‘fruits.jpg’ file from internet and save it in the same folder.

Comping the program using g++:

1. Use following command to compile:

g++ -Wall helloworld.cpp `pkg-config --libs --cflags opencv` -o helloworld

‘pkg-config –libs –cflags opencv’ adds all the necessary header file path and libraries.

2. run using ‘./helloworld’

OpenCV Installation Issue on Ubuntu: How to fix the ‘include : No such file or directory’ error

To install the latest OpenCV from git we follow the instructions on :

But if you stumble on the ‘include<Eigen/Eigenvalues>: No such file or directory’ error during make here’s how to fix that:

1. Install package

sudo apt-get install libeigen3-dev

2. Then change the cmake command to:


3. do ‘make’ and ‘sudo make install’ as instructed.