How to eliminate multiple declaration error for a symbol present inboth libs( without modifying libs

R

Raman

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

santosh

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

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

Flash Gordon

santosh wrote, On 25/03/08 04:37:
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).
 
S

santosh

Flash said:
santosh wrote, On 25/03/08 04:37:

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!


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

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

David Thompson

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
 
R

Raman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top