problem in linking

Nick Clifton nickc@redhat.com
Fri Dec 1 15:09:00 GMT 2006


Hi Massimiliano,

> suppose I have the following line
> 
> gcc main.o f2.o -ll1 -ll2 -o dummy
> 
> and suppose that libraries are:
> libl1.a contains f11.o and f12.o
> libl2.a contains f21.o and f22.o
> 
> in main.c I call explicitly only functions from f11.o and f21.o, so
> linker avoids to output f12.o and f22.o in executable file.
> However I need f12.o to be outputted in executable file because I use
> implicitly some functions.
> 
> How can I force the linker to output f12.o from libl1.a to executable
> even if no function is called explicitly?
> Is it a problem in library creation? Do I need to add f12.o to libl1.a
> in a special way?

No.  It is a problem in that you have this "implicit" way of using f12.o 
that is not known to the linker.  Since the linker cannot tell that you 
need f12.o it does not include it in the executable.

> Do I need to add explicitly f12.o in linking command line?

That is one way to do it.

You could also add "-Wl,-u,<symbol>" to the gcc command line where 
<symbol> is a the name of global symbol defined in f12.o.

You could also change the gcc command line to be:

   gcc main.o -Wl,--whole-archive -ll1 -Wl,--no-whole-archive -ll2

assuming that you know that you will need all of the object files inside 
libl1.a.

You could also add an explicit reference to a symbol defined in f12.o to 
the source for main.o.  That way the linker would know that you need 
f12.o and would include it in the executable.


Cheers
   Nick



More information about the Binutils mailing list