Statically linking in a static library

Ian Lance Taylor iant@google.com
Tue Dec 20 05:38:00 GMT 2011


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

Ian



More information about the Gcc-help mailing list