[Top][All Lists]

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

Re: Linking with a static library.

From: Paul Pluzhnikov
Subject: Re: Linking with a static library.
Date: Sat, 22 Oct 2005 09:27:15 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

Magnus Jonneryd <address@hidden> writes:

>> What is the failing link line and what is the *exact* error message?
> g++ ... -o ../simpleGrid .o/simpleGrid.o -lBody ... -lTheatre ...
> libTheatre.a(Theatre.o)(.text+0x92): In function 
> `Rendering::Theatre::render()':
> : undefined reference to `Rendering::BodyFactory::render()'

It is clear from the this message that your problem is *exactly*
the one of incorrect library ordering (searching Chemistry bookshelf
before the Biology one) as explained here:

> I still can't figure out why I'm able to link if i instantiate an object of
> class BodyFactory in my main method, if i do that everything seems to work.

Because in that case the compiler generates code in the main object
which references BodyFactory, and the librarian dutifully pulls
BodyFactory.o from the libBody.a because BodyFactory* symbols are
now on her "needed" list.

> Can this have something to do with that the class is a sub-class of a
> template (the class LightSourceFactory is a sub-class of the same
> template)?

No, it can't.
You didn't really read the page I referred you to, did you?

> It also seems to work if I add the library libBody.a to the
> linker line, after libTheatre.a, for the life of me I can't figure out why.

It is *all* explained in that web page. You really ought to read it.

> I've read what you wrote (linker.html) and i thought i understood it, but
> apperently not.

Hmm, what didn't you understand?

Librarian comes to libBody.a, and doesn't pull BodyFactory.o because
there is nothing in her "needed" list that tells her she needs it.

She then searches libTheatre.a and pulls Theatre.o because it has been
referenced somewhere ...

*Now* she needs BodyFactory* symbols, but since she never re-visits
libBody.a, she never gets a chance to find definitions for them,
hence the link error.

> Something else that really bugs me is that if i use nm to find the symbols
> in the library i get this:
> nm -BC libBody.a | grep BodyFactory
> 76:BodyFactory.o:
> 92:00000060 T Rendering::BodyFactory::render()
> Doesn't this imply that the symbol exists and that i should be able to link
> to it? 

Yes it does, and yes you should, *provided* you link in correct order!

Since libTheatre.a refers to symbols in libBody.a, it should
*preceed* libBody.a on the link line. 

If libBody.a in turn references symbols from libTheatre.a, then
you have a badly-structured libraries, and the solution is to
either merge them into a single archive (this is the most portable
solution), use --begin-group/--end-group (GNU linker only), or list
the libraries multiple times. 

All of this is explained in linker.html.

In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]