470,602 Members | 1,677 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,602 developers. It's quick & easy.

Linkage of sections from multiple obj files

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!
Jun 27 '08 #1
6 1592
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.
Jun 27 '08 #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)
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
Jun 27 '08 #3
On May 19, 5:34 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
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.
Jun 27 '08 #4
On May 19, 2:34 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
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:ja*********@gmail.com
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
Jun 27 '08 #5
On May 19, 3:36 pm, The Architect <architect.mat...@gmail.comwrote:
On May 19, 5:34 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
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:ja*********@gmail.com
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
Jun 27 '08 #6

"James Kanze" <ja*********@gmail.comwrote in message
news:00**********************************@s50g2000 hsb.googlegroups.com...
(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

Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by qazmlp | last post: by
4 posts views Thread by Nimmi Srivastav | last post: by
47 posts views Thread by Richard Hayden | last post: by
10 posts views Thread by Mark A. Gibbs | last post: by
5 posts views Thread by pembed2003 | last post: by
4 posts views Thread by Peter Ammon | last post: by
3 posts views Thread by DarthContinent | last post: by
2 posts views Thread by Nagrik | last post: by
3 posts views Thread by Tricky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.