C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

error not caught at link time: should it be?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
nzanella@cs.mun.ca
Guest





PostPosted: Wed Jul 27, 2005 6:36 pm    Post subject: error not caught at link time: should it be? Reply with quote



Hello,

I have two .cpp files. The first one contains:

Foo *foo;

and in the second one contains:

extern Foo foo;

I am using a modified version of gcc
with Makefiles automatically created.
The strange thing is that the tools
go through the link phase without
detecting the error. Isn't it to
be expected that the error be
caught by the linker???

I feel somewhat surprised that
it didn't catch it. I guess
when the linker looks for
external symbols it doesn't
care about matching their
types (eg. pointer versus value).

Thanks!!!

Neil

Back to top
Victor Bazarov
Guest





PostPosted: Wed Jul 27, 2005 7:37 pm    Post subject: Re: error not caught at link time: should it be? Reply with quote



[email]nzanella (AT) cs (DOT) mun.ca[/email] wrote:
Quote:
I have two .cpp files. The first one contains:

Foo *foo;

and in the second one contains:

extern Foo foo;

The two 'foo' are different. Essentially you have overloading
of the symbol 'foo' here.

Quote:
I am using a modified version of gcc
with Makefiles automatically created.

Irrelevant here.

Quote:
The strange thing is that the tools
go through the link phase without
detecting the error. Isn't it to
be expected that the error be
caught by the linker???

Linking is not part of the language specification.

Quote:
I feel somewhat surprised that
it didn't catch it. I guess
when the linker looks for
external symbols it doesn't
care about matching their
types (eg. pointer versus value).

Since you didn't post _how_ your 'foo' symbols are used, there is
nothing that can be said about it.

V

Back to top
Old Wolf
Guest





PostPosted: Wed Jul 27, 2005 10:45 pm    Post subject: Re: error not caught at link time: should it be? Reply with quote



Victor Bazarov wrote:
Quote:
nzanella (AT) cs (DOT) mun.ca wrote:
I have two .cpp files. The first one contains:

Foo *foo;

and in the second one contains:

extern Foo foo;

The two 'foo' are different. Essentially you have overloading
of the symbol 'foo' here.

If the first is at file scope, then it is an ODR violation,
causing undefined behaviour.

A similar situation occurs in the FAQ: one TU contains
char foo[9];
and the other contains
extern char *foo;
which causes undefined behaviour; the latter should have been
extern char foo[];


Back to top
Victor Bazarov
Guest





PostPosted: Thu Jul 28, 2005 1:32 pm    Post subject: Re: error not caught at link time: should it be? Reply with quote

Old Wolf wrote:
Quote:
Victor Bazarov wrote:

[email]nzanella (AT) cs (DOT) mun.ca[/email] wrote:

I have two .cpp files. The first one contains:

Foo *foo;

and in the second one contains:

extern Foo foo;

The two 'foo' are different. Essentially you have overloading
of the symbol 'foo' here.


If the first is at file scope, then it is an ODR violation,
causing undefined behaviour.

I am still trying to understand (after an evening of thinking about it)
why it would be an ODR violation. The first statement is a declaration
and a *definition* of a pointer, if it's at the namespace level; it has
static storage duration and as such is zero-initialised. So, it's
a null pointer to Foo. Are you saying it's defined elsewhere again?

The second statement is a declaration of a global object of type Foo
defined elsewhere. If there is an ODR violation, it's the second one,
the object, who isn't defined at all. And it only is an error if the
object is _used_, which we haven't been able to see.

Quote:
A similar situation occurs in the FAQ: one TU contains
char foo[9];
and the other contains
extern char *foo;
which causes undefined behaviour; the latter should have been
extern char foo[];

Again, only if the name is used.

V

Back to top
msalters
Guest





PostPosted: Thu Jul 28, 2005 4:56 pm    Post subject: Re: error not caught at link time: should it be? Reply with quote

Victor Bazarov schreef:
Quote:
Linking is not part of the language specification.

Of course it is. It's the part in which Translation Units are
combined. Like any other part of the standard, there is no
implementation detail mandated, but the phase must be there
in some form. In particular, a _linker_ is not part of the
specs, if the compiler can do it. Same as the preprocessor:
doesn't have to be a standalone tool, but the action must be
performed.

HTH,
Michiel Salters


Back to top
Old Wolf
Guest





PostPosted: Thu Jul 28, 2005 9:26 pm    Post subject: Re: error not caught at link time: should it be? Reply with quote

Victor Bazarov wrote:
Quote:
Old Wolf wrote:
Victor Bazarov wrote:

[email]nzanella (AT) cs (DOT) mun.ca[/email] wrote:

I have two .cpp files. The first one contains:

Foo *foo;

and in the second one contains:

extern Foo foo;

The two 'foo' are different. Essentially you have overloading
of the symbol 'foo' here.

If the first is at file scope, then it is an ODR violation,
causing undefined behaviour.

I am still trying to understand (after an evening of thinking about it)
why it would be an ODR violation. The first statement is a declaration
and a *definition* of a pointer, if it's at the namespace level; it has
static storage duration and as such is zero-initialised. So, it's
a null pointer to Foo. Are you saying it's defined elsewhere again?

The second statement is a declaration of a global object of type Foo
defined elsewhere. If there is an ODR violation, it's the second one,
the object, who isn't defined at all. And it only is an error if the
object is _used_, which we haven't been able to see.

Yes, I think ODR violation would be the wrong term.
I can't seem to find relevant text in the Standard that
deals with this issue though.

Quote:
A similar situation occurs in the FAQ: one TU contains
char foo[9];
and the other contains
extern char *foo;
which causes undefined behaviour; the latter should have been
extern char foo[];

Again, only if the name is used.

OK.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.