I have two libs (libFirst.a and libSecond.a). Both libs contains a
common function func(). Now I want to link an application "app" with
these two libs as
gcc -o app libFirst.a libSecond.a app.c.
It gives( and it should ) multiple declaration error . Is there any
way to tell the linker which func() declaration it must select.
NOTE: app need to be linked two both these libs.
Thanks and regards,
Raman Chalotra.
You can use the '-z muldefs' option to ld and by placing the archive
from which you want the reference to be resolved before the other
archive on the command line you can force ld to use the file that you
want to resolve symbol references.
In any case you should probably read the documentation for ld. Try 'info
ld' at the command prompt.
You are assuming that the OP is using gcc on whichever implementation
has an ld that supports that option. I have used gcc on AIX (two
different versions), SCO (two different versions), Linux and Windows, of
those only the Linux version used the GNU version of ld. AIX does it
differently and only under some conditions, on SCO -z means "do not bind
anything to address zero" and handles the OPs problem differently to
both AIX and Linux. Note that libraries with the extension .a are used a
*lot* more on AIX than on Linux!
> In any case you should probably read the documentation for ld. Try 'info
> ld' at the command prompt.
Or "man ld" since more implementations running gcc have man than have
info. Also asking on a group specific to the implementation in question
would be a good idea (probably *not* comp.unix.programmer as it varies
from one Unix variant to the next).
--
Flash Gordon
Yes. Point taken. I should have simply redirected the OP.
> I have two libs (libFirst.a and libSecond.a). Both libs contains a
> common function func(). Now I want to link an application "app" with
> these two libs as
>
> gcc -o app libFirst.a libSecond.a app.c.
>
> It gives( and it should ) multiple declaration error . Is there any
> way to tell the linker which func() declaration it must select.
>
Small but important terminology point: multiple _definition_.
A _definition_ in C provides the actual entity (an object, commonly
called a variable, or a function), and having more than one for the
same external name is a problem. Although one that the C standard does
not require an implementation to detect; you are mildly lucky that
your implementation did.
A _declaration_ is _sometimes_ (also) a definition, but when it isn't,
it only describes something defined elsewhere. It is definitely OK to
have external declarations in different T.U.s for the same external
entity, and that's the only (direct) way you can share it; it is also
legal to have multiple (compatible) declarations for the same external
entity in one T.U, and sometimes convenient.
That said, others have addressed the substance of your question.
- formerly david.thompson1 || achar(64) || worldnet.att.net
Taken a note of my ignorance.
Thanks,
Raman Chalotra