On Fedora dynamic linking is performed by ld-linux.so.2.
The dynamic linker use /etc/ld.so.cache and /etc/ld.so.preload to find library files.
Run ldconfig to tell the system where libfoo should look for libbar.
ldconfig looks in /lib, /usr/lib and any directory listed in /etc/ld.so.conf.
You can check which libraries a program uses with ldd.
More details are available on the manual pages for each command.
Here is an example of an application using shared libraries.
Program.cc
#include "foo.h"
#include <iostream>
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; ++i) {
std::cout << func_foo(argv[i]) << std::endl;
}
}
foo.h
#ifndef FOO_H
#define FOO_H
#include <string>
std::string func_foo(std::string const &);
#endif
foo.cc
#include "foo.h"
std::string func_foo(std::string const &arg)
{
return arg + "|" + __func__;
}
bar.h
#ifndef BAR_H
#define BAR_H
#include <string>
std::string func_bar();
#endif
bar.cc
#include "bar.h"
std::string func_bar()
{
return __func__;
}
Build with libfoo.so as a shared library.
g++ -Wall -Wextra -fPIC -shared foo.cc -o libfoo.so
g++ -lfoo -L./ -Wall -Wextra program.cc foo.h -o program
ldd program
...
libfoo.so => not found
Update /etc/ld.so.cache
sudo ldconfig /home/tobias/projects/stubs/so/
ldd shows that the dynamic linker finds libfoo.so
ldd program
...
libfoo.so => /home/tobias/projects/stubs/so/libfoo.so (0x00007f0bb9f15000)
Add a call to libbar.so in libfoo.so
New foo.cc
#include "foo.h"
#include "bar.h"
std::string func_foo(std::string const &arg)
{
return arg + "|" + __func__ + "|" + func_bar();
}
Build libbar.so and rebuild libfoo.so
g++ -Wall -Wextra -fPIC -shared bar.cc -o libbar.so
g++ -Wall -Wextra -fPIC -shared libbar.so foo.cc -o libfoo.so
ldd libfoo.so
...
libbar.so => not found
ldd program
...
libfoo.so => /home/tobias/projects/stubs/so/libfoo.so (0x00007f49236c7000)
libbar.so => not found
This shows that the dynamic linker still finds libfoo.so but not libbar.so
Again update /etc/ld.so.cache and recheck.
sudo ldconfig /home/tobias/projects/stubs/so/
ldd libfoo.so
...
libbar.so => /home/tobias/projects/stubs/so/libbar.so (0x00007f935e0bd000)
ldd program
...
libfoo.so => /home/tobias/projects/stubs/so/libfoo.so (0x00007f2be4f11000)
libbar.so => /home/tobias/projects/stubs/so/libbar.so (0x00007f2be4d0e000)
Both libfoo.so and libbar.so are found.
Note this last step have no effect on the application program.
If you are really strict running ldconfig is kind of relinking.
Weird or not the linker need to know the dependencies of the libraries it links.
There are a lot of other ways to implement this but this was chosen.