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

Discussion in 'C Programming' started by Raman, Mar 25, 2008.

  1. Raman

    Raman Guest

    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.
     
    Raman, Mar 25, 2008
    #1
    1. Advertising

  2. Raman

    santosh Guest

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

    Raman wrote:

    > 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.
     
    santosh, Mar 25, 2008
    #2
    1. Advertising

  3. Raman

    Flash Gordon Guest

    Re: How to eliminate multiple declaration error for a symbol presentin both libs( without modifying libs)

    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
     
    Flash Gordon, Mar 25, 2008
    #3
  4. Raman

    santosh Guest

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

    Flash Gordon wrote:

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


    Yes. Point taken. I should have simply redirected the OP.
     
    santosh, Mar 25, 2008
    #4
  5. Re: How to eliminate multiple declaration error for a symbol present in both libs( without modifying libs)

    On Mon, 24 Mar 2008 21:21:09 -0700 (PDT), 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.
    >

    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
     
    David Thompson, Apr 7, 2008
    #5
  6. Raman

    Raman Guest

    Re: How to eliminate multiple declaration error for a symbol presentin both libs( without modifying libs)

    On Apr 7, 11:00 am, David Thompson <> wrote:
    > On Mon, 24 Mar 2008 21:21:09 -0700 (PDT), 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.

    >
    > 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
     
    Raman, May 9, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page