mora
mora

Reputation: 2287

How can I link dynamic library depending on another dynamic library?

I make a program using a dynamic library, libexample.so. The dynamic library depends on another dynamic library, libtool.so.

It looks linker succeeded linking the libexample.so because of message from gcc.

Building target: libexample.so
Invoking: GCC C++ Linker
g++ -L/home/takehiro/Documents/documents/code/lib/tool -shared -o "libexample.so"  ./classes/example.o ./classes/example_template.o ./classes/example_test.o ./classes/impl.o   -ltool
Finished building target: libexample.so

cp libexample.so /home/takehiro/Documents/documents/code/lib/example

However, it failed to link it with the libtool.so.

ldd /home/takehiro/Documents/documents/code/lib/example/libexample.so 
    ...
    libtool.so => not found
    ...

I checked existence of the libtool.so under /home/takehiro/Documents/documents/code/lib/tool which is pointed by -L optoin at above linker by

ls /home/takehiro/Documents/documents/code/lib/tool

libtool.so

This is the first time to use a dynamic library depending another dynamic library. So I am so confused. Is it normal or malfunction? Why it cannot link them? Does someone have a suggestion or a solution to me? I am very glad it. Thank you very much.

Upvotes: 1

Views: 671

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118425

All that the -L option does is tell the linker where the shared library is, at link time.

This does not have any effect on where the runtime loader searches for shared libraries. That's why the shared library fails to be loaded at runtime.

You also need to pass the -rpath option to the linker, when you're linking your shared library, in order to set the RPATH attribute on the shared library, that specifies where its dependencies are to be searched for. Something like

g++ -L/home/takehiro/Documents/documents/code/lib/tool \
    -Wl,-rpath=/home/takehiro/Documents/documents/code/lib/tool \
    ... remaining options

Upvotes: 3

Related Questions