public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Problem with static library linking
@ 2008-03-24 12:38 Harish
  2008-03-24 13:00 ` Eljay Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Harish @ 2008-03-24 12:38 UTC (permalink / raw)
  To: gcc-help

Hi,

I am having a problem with the static library as explained below.


There was an existing static library libpcap.a 
I created a new static library with the name mylib.a , mylib.a calls a function func() in libpcap.a.

I created mylib.a using the below command

ar rcs mylib.a *.o libpcap.a
ranlib mylib.a

mylib.a has been successfully made. 

But when I use mylib.a with an executable it says undefined symbol for the function func() in the file which is present in mylib.a.

I used the command "nm mylib.a | grep func" to verify that mylib.a actually contains func().

But still I am getting linking error.

In my link line  libpcap.a comes before mylib.a

Could you please help.

I would be very thankful.

-Harish


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

* Re: Problem with static library linking
  2008-03-24 12:38 Problem with static library linking Harish
@ 2008-03-24 13:00 ` Eljay Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2008-03-24 13:00 UTC (permalink / raw)
  To: Harish, GCC-help

Hi Harish,

> In my link line libpcap.a comes before mylib.a

Since mylib.a depends on libpcap.a, mylib.a should come before libpcap.a on
your link line.

Otherwise, unfulfilled symbols in mylib.a won't be found because libpcap.a
had already been processed.

Order of archives on the link line is significant.

If you have circular dependencies in your archive libraries (ugh! Avoid if
possible!), you can do:

gcc -o myapp \
 foo.c \
 alpha.a \
 beta.a \
 alpha.a

...or on many platforms that use GNU ld...:

gcc -o myapp \
 foo.c \
 --Wl,--whole-archive \
   alpha.a beta.a \
 --Wl,--no-whole-archive

HTH,
--Eljay

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

end of thread, other threads:[~2008-03-24 13:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-24 12:38 Problem with static library linking Harish
2008-03-24 13:00 ` Eljay Love-Jensen

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).