Cannot open shared object file

Sterling picture Sterling · Nov 7, 2011 · Viewed 52.2k times · Source

I am trying to compile one of the projects found here USB-I2C/SPI/GPIO Interface Adapter.

I downloaded the i2c_bridge-0.0.1-rc2.tgz package. I installed libusb and that seemed to go well with no issues. I go into the i2c_bridge-0.0.1-rc2/ directory and make. That compiles. I move into the i2c_bridge-0.0.1-rc2/i2c folder and make. It compiles and gives me ./i2c. However, when I run it, it says error while loading shared libraries: libi2cbrdg.so: cannot open shared object file: No such file or directory

The makefile in i2c_bridge-0.0.1-rc2/i2c has the library directory as ../. The libi2cbrdg.so is in this directory (i2c_bridge-0.0.1-rc2). I also copied the file to /usr/local/lib. An ls of the i2c_bridge-0.0.1-rc2/ directory is

i2c        i2cbrdg.d  i2cbrdg.o  libi2cbrdg.a   Makefile  tests
i2cbrdg.c  i2cbrdg.h  INSTALL    libi2cbrdg.so  README    u2c4all.sh

(That i2c is a directory)

If I sudo ./i2c, it still gives me the problem.

I had to take away the -Werror and -noWdecrepated (spelling?) options in all the makefiles to get them to compile, but that shouldn't affect this should it?

What else is necessary for it to find the .so file? If anyone can help me find out what is wrong I would be very grateful. If more information is needed I can post it.

Answer

thiton picture thiton · Nov 7, 2011

You have to distinguish between finding so's at compile-time and at run-time. The -L flag you give at compile-time has nothing to do with localizing the library at run-time. This is rather done via a number of variables and some paths embedded in the library.

The best hot-fix for this problem is often setting LD_LIBRARY_PATH to the directory with the .so file, e.g.:

 $ LD_LIBRARY_PATH=.. ./i2c

For a long-term solution, you need to either have a close look at the whole LD system with rpath and runpath, or use libtool (which solves these issues for your portably).

Copying a file to /usr/local/lib is often insufficient because ld caches the available libraries, so you need to re-run ldconfig (as root) after you copied a library to /usr/local/lib.