Static Linking and Library Dependencies

Nick Clifton nickc@redhat.com
Tue Jul 18 09:44:00 GMT 2006


Hi Tom,

> Say I have two static libraries A and B.
> In A there is only one function, foo, that depends upon B.
> My program uses functions in A other than foo.  
> Is there any way to link my program with A but not have to link B also?

Yes.

I am tempted to just leave the answer there, but I will be nice.  If the 
other functions in A which are used by your program are in different 
object files within the library then B will not be linked it at all, and 
  you will not have to do any extra work.

If there are functions in A which are used by your program and which are 
in the same file(*) as foo then you have two choices.  If you have 
access to the sources for A then you can recompile it with the 
-ffunction-sections switch and then link with the --gc-sections switch. 
  This should allow foo and its dependencies in B to be discarded by the 
linker.  (Alternatively you can hand edit the sources for A so that foo 
is compiled in its own file).

If you do not have the sources for A then you can create a dummy version 
of the B library that is empty apart from the symbols needed to satisfy 
the references in foo().  Then you can use this dummy library in place 
of B.  This solution is not as elegant but it should work.

Cheers
   Nick

(*) Strictly speaking I should be talking about "compilation units" here 
rather than "files".



More information about the Binutils mailing list