public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Statically linking in a static library
@ 2011-12-19 21:21 Anthony Weston
  2011-12-20  5:38 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Anthony Weston @ 2011-12-19 21:21 UTC (permalink / raw)
  To: gcc-help

Hi,

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.

Thank you,
Tony

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Statically linking in a static library
  2011-12-19 21:21 Statically linking in a static library Anthony Weston
@ 2011-12-20  5:38 ` Ian Lance Taylor
  2011-12-20 16:02   ` Joel Dice
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Lance Taylor @ 2011-12-20  5:38 UTC (permalink / raw)
  To: Anthony Weston; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Statically linking in a static library
  2011-12-20  5:38 ` Ian Lance Taylor
@ 2011-12-20 16:02   ` Joel Dice
  0 siblings, 0 replies; 3+ messages in thread
From: Joel Dice @ 2011-12-20 16:02 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Anthony Weston, gcc-help

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-12-20 15:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-19 21:21 Statically linking in a static library Anthony Weston
2011-12-20  5:38 ` Ian Lance Taylor
2011-12-20 16:02   ` Joel Dice

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).