Linkage of sections from multiple obj files

Discussion in 'C++' started by The Architect, May 19, 2008.

  1. Hi,

    If I have the same symbol in the .data section of 2 obj files, LD
    gives a multiple declaration error on linking? Would like to know the
    reason for this (diab only issues a warning)

    Also what if the symbols were in any other section (.rdata or .text)?
    Or which sections of obj files can be merged even if they have
    overlapping symbol names?

    TIA!
     
    The Architect, May 19, 2008
    #1
    1. Advertising

  2. The Architect wrote:
    > If I have the same symbol in the .data section of 2 obj files, LD
    > gives a multiple declaration error on linking? Would like to know the
    > reason for this (diab only issues a warning)


    You have implemented the same function or global variable (or a
    variable inside a namespace) in more than one compilation unit.

    If the same function must appear in more than one compilation unit
    (for example because it's implemented in a header file) you have to make
    that function 'inline' (with that keyword). That will tell the linker
    that it's ok if it appears in more than one object file. (Template
    functions are implicitly inline.)

    As for the variables, only one global instance must exist. Either
    rename the variables in the different compilation units to avoid the
    name collision, or if all of them must refer to the *same* global
    variable, declare it 'extern' in all but one of the compilation units.
    (Although we don't use global variables, do we?)

    If none of this applies to your case, then perhaps you have some kind
    of problem with project dependencies? Try rebuilding everything.
     
    Juha Nieminen, May 19, 2008
    #2
    1. Advertising

  3. On May 19, 5:34 pm, Victor Bazarov <> wrote:
    > The Architect wrote:
    > > If I have the same symbol in the .data section of 2 obj files, LD
    > > gives a multiple declaration error on linking? Would like to know the
    > > reason for this (diab only issues a warning)

    >
    > I would *guess* is that LD is not designed to handle multiple
    > definitions of the same symbol. BTW, that is what the C++ language
    > Standard also requires: one and only one definition of any symbol in
    > your program. So, your linker seems to be enforcing the ODR.
    >
    > > Also what if the symbols were in any other section (.rdata or .text)?

    >
    > The term "section" is not defined by the language Standard.
    >
    > > Or which sections of obj files can be merged even if they have
    > > overlapping symbol names?

    >
    > That's seems like a question you need to ask in the compiler- or
    > platform-specific newsgroup.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Ok.
    Thanks for all the insights.
    I did manage to resolve the problem by making it const (the symbol
    moved to the .rdata section). Was curious as to how the linker behaved
    with this change.
    Have posted this in the GCC group.
     
    The Architect, May 19, 2008
    #3
  4. The Architect

    James Kanze Guest

    On May 19, 2:34 pm, Victor Bazarov <> wrote:
    > The Architect wrote:
    > > If I have the same symbol in the .data section of 2 obj files, LD
    > > gives a multiple declaration error on linking? Would like to know the
    > > reason for this (diab only issues a warning)


    > I would *guess* is that LD is not designed to handle multiple
    > definitions of the same symbol. BTW, that is what the C++ language
    > Standard also requires: one and only one definition of any symbol in
    > your program. So, your linker seems to be enforcing the ODR.


    Only very, very partially. It probably won't complain if he has
    `extern double x;' in once module, and `int x;' in another.

    > > Also what if the symbols were in any other section (.rdata
    > > or .text)?


    > The term "section" is not defined by the language Standard.


    And the usual word for them is "segment":).

    Classically, a linker will expect that there is one, and only
    one, definition for each symbol, regardless of the segment it's
    in. Most linkers also support other conventions as well,
    depending on how the symbol is declared in the object file
    (which, of course, is up to the compiler): support for Fortran's
    common data or for multiple template instantiations (usually by
    means of weak references) comes to mind. (Some early C
    compilers declared each global variable as if it were a Fortran
    common data; this isn't conform with the standard, however.)

    > > Or which sections of obj files can be merged even if they
    > > have overlapping symbol names?


    > That's seems like a question you need to ask in the compiler-
    > or platform-specific newsgroup.


    While this isn't really the place for such questions, there is a
    lot of commonality in how linkers work---comp.compilers might be
    an acceptable place. (I would suggest a good book about linking
    first, but I don't know of one off hand.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 20, 2008
    #4
  5. The Architect

    James Kanze Guest

    On May 19, 3:36 pm, The Architect <> wrote:
    > On May 19, 5:34 pm, Victor Bazarov <> wrote:
    > > The Architect wrote:
    > > > If I have the same symbol in the .data section of 2 obj
    > > > files, LD gives a multiple declaration error on linking?
    > > > Would like to know the reason for this (diab only issues a
    > > > warning)


    > > I would *guess* is that LD is not designed to handle
    > > multiple definitions of the same symbol. BTW, that is what
    > > the C++ language Standard also requires: one and only one
    > > definition of any symbol in your program. So, your linker
    > > seems to be enforcing the ODR.


    > > > Also what if the symbols were in any other section (.rdata
    > > > or .text)?


    > > The term "section" is not defined by the language Standard.


    > > > Or which sections of obj files can be merged even if they
    > > > have overlapping symbol names?


    > > That's seems like a question you need to ask in the
    > > compiler- or platform-specific newsgroup.


    > I did manage to resolve the problem by making it const (the
    > symbol moved to the .rdata section). Was curious as to how the
    > linker behaved with this change.


    Don't confuse what the compiler and linker are doing with what
    you do in your C++ code to trigger this behavior. In C++, a
    const variable at namespace scope has internal linkage by
    default, a non-const variable at namespace scope has external
    linkage by default. At the C++ level, you've changed the
    linkage.

    If the variable is in a header file, you've changed the number
    of instances of it: with internal linkage, you have one instance
    per translation unit; with external, you have one instance in
    all.

    As to what the compiler does with it: with external linkage, if
    the declaration is a definition, the compiler generates extra
    information for the linker, telling it that the symbol is
    globally defined; normally, this will result in an error if
    there are duplicate definitions. With internal linkage, the
    compiler doesn't generate this information---as far as the
    linker is concerned, the symbol doesn't exist. The segment
    where the object corresponding to the symbol is irrelevant here.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 20, 2008
    #5
  6. The Architect

    kwikius Guest

    "James Kanze" <> wrote in message
    news:...

    > (I would suggest a good book about linking
    > first, but I don't know of one off hand.)


    Linkers and Loaders by John R. Levine

    Nice quote from the Preface

    "All the linker writers in the world could probably fit in one room and half
    of them have already because they reviewed the manuscript " . (Slight
    misquote but nice effect ).

    regards
    Andy Little
     
    kwikius, May 20, 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.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,855
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    487
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    461
    Mike C. Fletcher
    Feb 20, 2004
  4. Raman
    Replies:
    6
    Views:
    4,844
    santosh
    Aug 3, 2007
  5. Replies:
    1
    Views:
    613
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page