59

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 || !image.data)
   {
       cout << "No image data\n";
       return -1;
   }

   namedWindow("Display Image",CV_WINDOW_AUTOSIZE);
   imshow("Display Image",image);
   waitKey(0);
   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: libopencv_core.so.2.4: cannot open shared object file: No such file or directory
mtsvetkov
  • 885
  • 10
  • 21
linkrules
  • 701
  • 1
  • 6
  • 5
  • Logically libopencv_core.so.2.4 should be in /usr/local/lib, can you find it there? – Sassa Sep 09 '12 at 01:42
  • Include the path, where you opencv-so files are located in LD_LIBRARY_PATH. (or ldconfig may also help) – Valentin H Sep 14 '12 at 11:12
  • 1
    I just leave this here as a comment. I had the same problem `sudo apt-get update` and `sudo apt-get upgrade` solve my problem. – endertunc Mar 07 '16 at 16:58

5 Answers5

108

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/ld.so.conf.d/opencv.conf 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/libopencv_core.so.2.4 then I would write this to my opencv.conf file:

/usr/local/opencv/

Then run

sudo ldconfig -v

If you can't find the libraries, try running

sudo updatedb && locate libopencv_core.so.2.4

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

References:

About shared libraries on Linux: http://www.eyrie.org/~eagle/notes/rpath.html

About adding the OpenCV shared libraries: http://opencv.willowgarage.com/wiki/InstallGuide_Linux

Kasparov92
  • 1,365
  • 4
  • 14
  • 39
Cookyt
  • 1,607
  • 1
  • 12
  • 12
  • 4
    Usually, **opencv** libraries are installed in **/usr/local/lib/**. I had a similar problem, and I solved it deleting some **opencv** pre-compiled packages. – Thomio Sep 14 '15 at 16:48
  • 1
    As thomio mentioned, removing pre-combiled packages solved the problem for me too. Eg. on ubuntu L4T `dpkg -l | grep opencv` and then `sudo apt-get remove --purge libopencv4tegra` for the Jetson TK1 – RangerJo Apr 05 '16 at 09:13
  • Thx! it works for me. For those of you don't know what "ldconfig" command is [ldconfig - configure dynamic linker run-time bindings](https://linux.die.net/man/8/ldconfig) – WY Hsu Aug 30 '17 at 13:46
23

To make it more clear(and to put it together) I had to do Two things mentioned above.

1- Create a file /etc/ld.so.conf.d/opencv.conf and write to it the paths of folder where your opencv libraries are stored.(Answer by Cookyt)

2- Include the path of your opencv's .so files in LD_LIBRARY_PATH ()

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv/lib
Umair R
  • 820
  • 8
  • 16
20

Find the folder containing the shared library libopencv_core.so.2.4 using the following command line.

sudo find / -name "libopencv_core.so.2.4*"

Then I got the result:

 /usr/local/lib/libopencv_core.so.2.4.

Create a file called /etc/ld.so.conf.d/opencv.conf and write to it the path to the folder where the binary is stored.For example, I wrote /usr/local/lib/ to my opencv.conf file. Run the command line as follows.

sudo ldconfig -v

Try to run the command again.

codeislife
  • 319
  • 2
  • 8
  • 2
    Just wanted to pitch in; in my case, the library was actually installed in `/usr/local/lib/` but I got the same error as OP. All I had to do was run `sudo ldconfig -v`, I did not need to create the `/etc/ld.so.conf.d/opencv.conf` (because of where the library was installed). – birgersp Mar 02 '18 at 13:00
18

Umair R's answer is mostly the right move to solve the problem, as this error used to be caused by the missing links between opencv libs and the programme. so there is the need to specify the ld_libraty_path configuration. ps. the usual library path is suppose to be:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

I have tried this and it worked well.

Andol Li
  • 211
  • 2
  • 6
0

Add this link:

/usr/local/lib/*.so.*

The total is:

g++ -o main.out main.cpp -I /usr/local/include -I /usr/local/include/opencv -I /usr/local/include/opencv2 -L /usr/local/lib /usr/local/lib/*.so /usr/local/lib/*.so.*
misakawa
  • 41
  • 1
  • 6