Error while loading shared libraries

cdynamic-linkinggentoolinker

My project tree looks something like that:

src/
include/
Makefile
lib/
   lib/3rdparylib/

I didn't code 3rdpartylib, but decided to embed its source code in my project packaging. I compile the software by doing the following steps:

$ cd lib/3rdpartylib/
$ make
$ ln -s 3rdpartylib.so.0 3rdpartylib.so

Then I compile each of my source files like this:

$ gcc -c src/file.c -I include -o file.o -l 3rdparylib -L lib/3rdpartylib -I lib/3rdpartylib/include

Then I link:

$ gcc file1.o file2.o -l3rdpartylib -L lib/3rdpartylib -o myapp

When I am on my main machine, it works perfectly.
Today I tried to launch it on another machine. It compiled and linked without any problem. However when I tried to launch the application I got the following error message.

./myapp: error while loading shared libraries: 3rdpartylib.so.0: cannot open shared object file: No such file or directory

I tried doing the following:

export LD_LIBRARY_PATH=/path/to/3rdpartylib.so

It seems to work. But I understand that using LD_LIBRARY_PATH is a bad practice. It bothers me to have this variable set up every time I want to run my app.

What am I missing? Why does it work on my main machine (where LD_LIBRARY_PATH is not set to anything) and not on the other machine? Does it matter that the other machine is a virtual one?

If it is of any help, my main machine is a Debian box, and my "new" machine is a Sabayon (Gentoo), running in Virtualbox.

Best Answer

What matters is what Linux distribution each machine uses, because they handle library paths differently. On the Gentoo (Sabayon) machine, if you want a third party library to be usable system-wide, you should:

  • Create a file under /etc/env.d/ that will contain the additional environment setup. The files are named using the scheme [0-9][0-9]somename - the two initial digits decide on the order in which they are used. A good practice requires that your custom settings are added (near) last, so 99 is a proper beginning, as long as it works. Getting to the point: Create a file named like

    /etc/env.d/99mythirdpartylib
    

    containing

    LDPATH=/path/to/your/library
    
  • To make the changes active without reboot, run as root:

    env-update && source /etc/profile
    

    (which will update the environment as well as run ldconfig).

Reference, if you'd like one.