Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ODR: A simple question

0 views
Skip to first unread message

ebony...@gmail.com

unread,
Sep 29, 2008, 6:01:07 AM9/29/08
to
Dear all
Hi

I encountered a simple but IMO important problem about the C++ linkage
model and One Definition Rule. Why the following code link?

file1.cpp
int x;

file2.cpp
double x;

x was defined in two different translation units with different types.
It breaks the ODR.
The following code is also linked:

file1.cpp
int x;

file2.cpp
extern double x;
It is obvious the type x in declaration and definition are different.
I ran and tested codes in Visual Studio 6.0 and .Net 2005.

Thank you in advance
- Saeed Amrollahi

Pete Becker

unread,
Sep 29, 2008, 7:17:15 AM9/29/08
to
On 2008-09-29 06:01:07 -0400, ebony...@gmail.com said:

>
> I encountered a simple but IMO important problem about the C++ linkage
> model and One Definition Rule. Why the following code link?
>
> file1.cpp
> int x;
>
> file2.cpp
> double x;
>
> x was defined in two different translation units with different types.
> It breaks the ODR.

The language definition doesn't require compilers to diagnose
violations of the ODR. Code like this has undefined behavior. As a
practical matter, recognizing errors like this is expensive, given
C++'s separate compilation model.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

James Kanze

unread,
Sep 29, 2008, 9:44:59 AM9/29/08
to
On Sep 29, 1:17 pm, Pete Becker <p...@versatilecoding.com> wrote:

> On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
> > I encountered a simple but IMO important problem about the
> > C++ linkage model and One Definition Rule. Why the following
> > code link?

> > file1.cpp
> > int x;

> > file2.cpp
> > double x;

> > x was defined in two different translation units with
> > different types. It breaks the ODR.

> The language definition doesn't require compilers to diagnose
> violations of the ODR. Code like this has undefined behavior. As a
> practical matter, recognizing errors like this is expensive, given
> C++'s separate compilation model.

And the relatively low quality of most linkers. It wouldn't be
very hard to implement, if the linker supported it. For
historical reasons, most linkers don't.

--
James Kanze (GABI Software) email:james...@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

ebony...@gmail.com

unread,
Sep 30, 2008, 1:58:45 PM9/30/08
to
On Sep 29, 2:17 pm, Pete Becker <p...@versatilecoding.com> wrote:

Sorry for a day delay. I was out of office.
Thank you pete for you answer, but I didn't completely convince.
I thought may be some standard conversions were made (double to int or
vice versa)
I changed double to std::string, but program run. Just when I declare
the variable
with extern, I have to specifiy the type and compiler find the real
type.
Which clause or section of C++ standard draft mention your answer?

Regards,
- Saeed Amrollahi

ebony...@gmail.com

unread,
Sep 30, 2008, 2:06:14 PM9/30/08
to
On Sep 29, 4:44 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Sep 29, 1:17 pm, Pete Becker <p...@versatilecoding.com> wrote:
>
> > On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
> > > I encountered a simple but IMO important problem about the
> > > C++ linkage model and One Definition Rule. Why the following
> > > code link?
> > > file1.cpp
> > > int x;
> > > file2.cpp
> > > double x;
> > > x was defined in two different translation units with
> > > different types.  It breaks the ODR.
> > The language definition doesn't require compilers to diagnose
> > violations of the ODR. Code like this has undefined behavior. As a
> > practical matter, recognizing errors like this is expensive, given
> > C++'s separate compilation model.
>
> And the relatively low quality of most linkers.  It wouldn't be
> very hard to implement, if the linker supported it.  For
> historical reasons, most linkers don't.
>
> --
> James Kanze (GABI Software)             email:james.ka...@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

Sorry for a day delay. I was out of office.
Thank you for your answer. AFAIR, Stroustrup mentioned the poor
quality of linkers in 1st edition
of his book. Where can I find, C++ Linker specific information?

Regards,
Saeed Amrollahi

Triple-DES

unread,
Oct 1, 2008, 2:18:42 AM10/1/08
to
On 30 Sep, 19:58, ebony.s...@gmail.com wrote:
> On Sep 29, 2:17 pm, Pete Becker <p...@versatilecoding.com> wrote:
> > On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
>
> > > I encountered a simple but IMO important problem about the C++ linkage
> > > model and One Definition Rule. Why the following code link?
>
> > > file1.cpp
> > > int x;
>
> > > file2.cpp
> > > double x;
>
> > > x was defined in two different translation units with different types.
> > > It breaks the ODR.
>
> > The language definition doesn't require compilers to diagnose
> > violations of the ODR. Code like this has undefined behavior. As a
> > practical matter, recognizing errors like this is expensive, given
> > C++'s separate compilation model.
>
> Sorry for a day delay. I was out of office.
> Thank you pete for you answer, but I didn't completely convince.
> I thought may be some standard conversions were made (double to int or
> vice versa)
> I changed double to std::string, but program run. Just when I declare
> the variable
> with extern, I have to specifiy the type and compiler find the real
> type.
> Which clause or section of C++ standard draft mention your answer?
>

See 3.2 One definition rule [basic.def.odr]


> Regards,
>   - Saeed Amrollahi

James Kanze

unread,
Oct 1, 2008, 5:37:03 AM10/1/08
to
On Sep 30, 8:06 pm, ebony.s...@gmail.com wrote:
> On Sep 29, 4:44 pm, James Kanze <james.ka...@gmail.com> wrote:
> > On Sep 29, 1:17 pm, Pete Becker <p...@versatilecoding.com> wrote:

> > > On 2008-09-29 06:01:07 -0400, ebony.s...@gmail.com said:
> > > > I encountered a simple but IMO important problem about the
> > > > C++ linkage model and One Definition Rule. Why the following
> > > > code link?
> > > > file1.cpp
> > > > int x;
> > > > file2.cpp
> > > > double x;
> > > > x was defined in two different translation units with
> > > > different types. It breaks the ODR.
> > > The language definition doesn't require compilers to
> > > diagnose violations of the ODR. Code like this has
> > > undefined behavior. As a practical matter, recognizing
> > > errors like this is expensive, given C++'s separate
> > > compilation model.

> > And the relatively low quality of most linkers. It wouldn't
> > be very hard to implement, if the linker supported it. For
> > historical reasons, most linkers don't.

> Thank you for your answer. AFAIR, Stroustrup mentioned the poor


> quality of linkers in 1st edition
> of his book.

Yes. C++ was originally designed to use existing linkers, with
a minimum of additional baggage. (There was, IIRC, always a
"pre-linker", which generated the code for dynamic
initialization. But that was it.)

Today, of course, templates require a lot more support, and much
of it could be used to allow greater freedom elsewhere as well.

> Where can I find, C++ Linker specific information?

To tell the truth, I don't know. Linkers seem to be the poor
relative in the language translation area: you'll find all sorts
of information concerning compiler theory and the like, but very
little about linkers.

I think the question has been raised once or twice in
comp.compilers; you might try there. (Not because the question
is off subject here, but because that's where the people who
actually work on these things hang out.)

--
James Kanze (GABI Software) email:james...@gmail.com

0 new messages