RE: Creating/using shared libraries

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In addition, static libraries (archives) that you are linking to, should
appear _last_ on the command line - or at least, after all .o files and
shared libraries.  Otherwise, not all symbols from the static library
may get picked up properly.  Basically, when the linker looks at the
static library, it tries to resolve any currently unresolved symbols
using the static library.  Once it's done with the library, it
effectively throws it away and never looks at it again.  And it doesn't
keep a record of what symbols were defined in the library but that
weren't needed.  That means that if you have another library or .o file
that needs a symbol defined in the static library, but that comes after
it on the command line, the linker will not properly resolve the symbol.

For example, let's say that you have a program in prog.c and you're
using some routines from libroutines.a.  If you compile your program
like this:

gcc -c prog.c
gcc -o prog -lroutines prog.o

The linker will look at libroutines before looking at prog.o.  And when
it does, it will say, "there are currently no undefined symbols that are
defined in libroutine.a," and throw it away.  Then when it looks at
prog, it will add new undefined symbols to the symbol table (which are
defined in libroutine.a).  However, since it already processed
libroutine.a, it won't do it again, and it doesn't know that your
missing symbols can be found there.

So, you need to link it like this, instead:

gcc -o prog prog.o -lroutines

Make sense?

Cheers,
Lyle

-----Original Message-----
From: gcc-help-owner@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx] On
Behalf Of Sisyphus
Sent: Sunday, February 01, 2004 3:22 PM
To: Frank
Cc: GNU GCC help list
Subject: Re: Creating/using shared libraries

Frank wrote:
> Trying to  move from archive to shared libraries for an application. I

> believe I was able to create the shared library ok by compiling the 
> .cpp's with -fpic and using g++ -shared with the objects (.o's).  But 
> when I try to compile a program that used that library I get a
undefined 
> reference from the shared library about an archive library object that

> it was compiled with (it was included on the link fro both the shared 
> lib and the executable). Is there some other option/procedure I need
to 
> do to use the shared library?
> 
> Any good reference to understand how to build and use shared
libraries.  
> I'm using GNU gcc/make
> 

http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/libr
aries.html

You still need to link to the static library when building the app.

Hope I've understood you correctly.

Cheers,
Rob

-- 
Any emails containing attachments will be deleted from my ISP's mail 
server before I even get to see them. If you wish to email me an 
attachment, please provide advance warning so that I can make the 
necessary arrangements.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux