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


c++

linux

linker

shared-libraries

I am trying to build a shared library. Let us say libabc.so .It uses another .so
le , say lib123.so (a lib in /usr/local/lib) .Now i am using my shared lib
libabc.so 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 lib123.so directly. my-app
should be linked with only libabc.so. How can i do this?
Thanks in advance. I am using g++ compiler

share

improve this question


Anoop Ravi
541 4 16

Asked
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
libabc.so 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 libabc.so 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 libabc.so with


gcc -shared abc1.pic.o
l123 -o libabc.so

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/lib123.so shared library.

Read also the Program Library HowTo.


As you see, you may link a shared library lib123.so into your own
shared library libabc.so
Then check with ldd libabc.so
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 ld.so has to do
zillions of relocations.

share

improve this answer


Basile Starynkevitch
118k 7 84 187

Answered
Oct 17 '13 at 10:47
Edited
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
libabc.so , it is not listing lib123.so. 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 libabc.so 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
libabc.so Basile Starynkevitch Oct 17 '13 at 11:26

show 8 more comments

Your Answer

log in
or
Name

Email

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