Statically linking in a static library

Joel Dice joel.dice@gmail.com
Tue Dec 20 16:02:00 GMT 2011


On Mon, 19 Dec 2011, Ian Lance Taylor wrote:

> Anthony Weston <tony.weston@veliosystems.com> writes:
>
>> I'm having trouble with statically linking.  I've created a static
>> library that statically links to other static libraries.  My hope by
>> doing this was so other users would only need to include one static
>> library when linking in their executable.  However when I nm the
>> created the static library all the references are unresolved.  Is
>> there an argument that will force the linker to resolve these symbols?
>>
>> Example:
>>
>> libA.a defines functionA.
>> libB.a defines functionB.
>>
>> I create libC.a which statically links and uses both libA.a and libB.a.
>>
>> When running nm libC.a the results show that neither functionA nor
>> functionB can be resolved so libA.a and libB.a also need to be
>> statically linked into the final executable.
>
> Yes, that is how Unix linkers work.
>
> If you want the final program to be able to only link against a single
> library, then you will need to incorporate libA.a and libB.a into
> libC.a.  E.g., do something along the lines of
>
> ar x libA.a
> ar x libB.a
> ar rc libC.a *.o

Be careful, though, about files with the same name (e.g. myobject.o in 
both libA.a and libB.a).  If they're really the same file, it won't matter 
which one you include, but if they're different, you may need to rename 
one so they're both included.



More information about the Gcc-help mailing list