Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to eliminate multiple declaration error for a symbol present in both libs( without modifying libs)

2 views
Skip to first unread message

Raman

unread,
Mar 25, 2008, 12:21:09 AM3/25/08
to
Hi All,


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.

santosh

unread,
Mar 25, 2008, 12:37:47 AM3/25/08
to
Raman wrote:

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.

Flash Gordon

unread,
Mar 25, 2008, 2:58:37 PM3/25/08
to
santosh wrote, On 25/03/08 04:37:

> Raman wrote:
>
>> 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.
>
> 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.

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

santosh

unread,
Mar 25, 2008, 3:08:02 PM3/25/08
to
Flash Gordon wrote:

Yes. Point taken. I should have simply redirected the OP.

David Thompson

unread,
Apr 7, 2008, 2:00:52 AM4/7/08
to
On Mon, 24 Mar 2008 21:21:09 -0700 (PDT), Raman
<ramanc...@gmail.com> wrote:

> 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

Raman

unread,
May 9, 2008, 2:12:42 AM5/9/08
to
On Apr 7, 11:00 am, David Thompson <dave.thomps...@verizon.net> wrote:
> On Mon, 24 Mar 2008 21:21:09 -0700 (PDT), Raman
>

Taken a note of my ignorance.

Thanks,
Raman Chalotra

0 new messages