You are on page 1of 3

sign up

log in

Stack Overow
Questions Tags Users Badges Unanswered Ask

Linking a shared library with another shared lib in linux





I am trying to build a shared library. Let us say .It uses another .so
le , say (a lib in /usr/local/lib) .Now i am using my shared lib in my application. say my-app.I want to know how i should link
these binaries??i don't want to link my-app with directly. my-app
should be linked with only How can i do this?
Thanks in advance. I am using g++ compiler


improve this question

Anoop Ravi
541 4 16

Oct 17 '13 at 10:41

have you thought about using an atuomated build system, for example autotools? autotools
comes with libtool, which takes most of the awkward processing required to build shared
libraries away. Andreas Grapentin Oct 17 '13 at 10:45

@nyarlathotep: you are wrong in advising to link with a static lib122.a the shared Basile Starynkevitch Oct 17 '13 at 10:49
Show your exact build commands (for the object les and the shared library). Basile
Starynkevitch Oct 17 '13 at 11:04
add a comment

1 Answer

Order By Votes

Suppose that is obtained from posiition independent

object code les abc1.pic.o and abc2.pic.o ; then you have built
them with e.g.
gcc -Wall -fPIC -O -g abc1.c -c -o abc1.pic.o
gcc -Wall -fPIC -O -g abc2.c -c -o abc2.pic.o

and you build with

gcc -shared abc1.pic.o
l123 -o

abc2.pic.o -L/usr/local/lib -

I added -L/usr/local/lib before -l123 because I am assuming you have a

/usr/local/lib/ shared library.

Read also the Program Library HowTo.

As you see, you may link a shared library into your own
shared library
Then check with ldd
PS. Don't use a static library for lib123.a (it should be PIC). If you
link non-PIC code into a shared object, you lose most of the
advantages of shared objects, and the dynamic linker has to do
zillions of relocations.


improve this answer

Basile Starynkevitch
118k 7 84 187

Oct 17 '13 at 10:47
Oct 17 '13 at 11:32

Why not a static library for lib123? What if it is not PIC? B Oct 17 '13 at 11:05

I had done exactly the same thing you mentioned here. But when i do a ldd on , it is not listing Do you know why it is like that? Anoop Ravi
Oct 17 '13 at 10:56

I cannot help, unless you show at least the precise commands involved in building
your abc*.pic.o objects and the library. Basile Starynkevitch Oct 17
'13 at 11:05
@B: Because static libraries are non-PIC in general, and are basically a
catenation of member object les. Basile Starynkevitch Oct 17 '13 at 11:06
Please edit your question. And tell there exactly how you build both abc.pic.o and Basile Starynkevitch Oct 17 '13 at 11:26

show 8 more comments

Your Answer

log in


By posting your answer, you agree to the privacy policy and

terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc