473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1737
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
4
by: Nimmi Srivastav | last post by:
Once and for all can someone kindly tell me the difference between C and C++ linkage. I thought I understood it till someone showed me the other day that C functions, that would ordinarily require...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
3
by: DarthContinent | last post by:
I have multiple websites running under Windows 2003 using IIS and ASP ..NET. I'd like to be able to take multiple Web.config files and parse sections of them out for display in a datagrid or to...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
3
by: Tricky | last post by:
When I try and link together 3 object files, I get the following problem: gcc fusionFileIO.o fusionAlgorithms.o main.o -o fusionTK.exe main.o:main.c:(.rdata+0x0): multiple definition of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.