openCV program compile error “ cannot open shared object file: No such file or directory” in ubuntu 12.04

I compiled and installed openCV 2.4.2 in ubuntu 12.04. Under /usr/local/include I can see the directories /usr/local/opencv and /usr/local/opencv2.

Here is the code I wrote:

#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc,char **argv)
   Mat image;
   image = imread(argv[1],1);

   if(argc != 2 || !
       cout << "No image data\n";
       return -1;

   namedWindow("Display Image",CV_WINDOW_AUTOSIZE);
   imshow("Display Image",image);
   return 0;

I compiled it using this command line:

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

There were no compile time errors, however when I try to run the resulting binary with /DisplayImage code.png I get the following error message:

./DisplayImage: error while loading shared libraries: cannot open shared object file: No such file or directory


You haven’t put the shared library in a location where the loader can find it. look inside the /usr/local/opencv and /usr/local/opencv2 folders and see if either of them contains any shared libraries (files beginning in lib and usually ending in .so). when you find them, create a file called /etc/ and write to it the paths to the folders where the libraries are stored, one per line.

for example, if the libraries were stored under /usr/local/opencv/ then I would write this to my opencv.conf file:


Then run

sudo ldconfig -v

If you can’t find the libraries, try running

sudo updatedb && locate

in a shell. You don’t need to run updatedb if you’ve rebooted since compiling OpenCV.


About shared libraries on Linux:

About adding the OpenCV shared libraries:

Source : Link , Question Author : linkrules , Answer Author : Kasparov92

Leave a Comment