compile error in C++, not C

T

Tim H

The following program compiles just fine in C, but not in C++. Can
anyone explain why? I have a chunk of code that defines stuff like
this in headers (without the extern) that I can not easily change.

The C compiler recognizes the first foo and second foo as the same.
The C++ compiler not so much.

Is there a way to get this to compile in C++ without changing all the
headers?


int foo;
int foo=1;

int
main(void)
{
return foo;
}
 
S

Salt_Peter

The following program compiles just fine in C, but not in C++. Can
anyone explain why? I have a chunk of code that defines stuff like
this in headers (without the extern) that I can not easily change.

The C compiler recognizes the first foo and second foo as the same.
The C++ compiler not so much.

A declaration is a declaration and these must be unique. C++ enforces
this with no exceptions whatsoever. You are suggesting that its the
programmer that should sift through all statements to figure out which
(and where) a given variable is declared.

consider:
int foo;
double foo(0);

also, how do you expect the client (the user of your code) to figure
out what you wrote above means? What if the client sees int foo = 1;
and doesn't realize that foo is in another scope? poof goes your
theory.
 
J

Juha Nieminen

Tim said:
The C compiler recognizes the first foo and second foo as the same.

This is just a personal opinion, but IMO the C language specification
is broken. I clearly see two variables being declared there, both with
the same name.
 
J

Jerry Coffin

The following program compiles just fine in C, but not in C++. Can
anyone explain why? I have a chunk of code that defines stuff like
this in headers (without the extern) that I can not easily change.

The C compiler recognizes the first foo and second foo as the same.
The C++ compiler not so much.

Is there a way to get this to compile in C++ without changing all the
headers?


int foo;
int foo=1;

In C, each of these is (separately) a "tentative definition". You can
have as many tentative definitions for a variable as you like, as long
as none of them directly conflicts with any other. After reading all the
tentative definitions, the compiler creates a complete definition of the
variable based on 1) the defaults from where those definitions are
placed, and 2) all the storage classes, qualifiers, etc. you put into
all the tentative definitions. As I said, as long as nothing you put
into one tentative definition directly conflicts with what you put in
another (e.g. defining foo as an int in one place but a double in
another) this is allowed. For example:

int foo;
extern int foo;
volatile int foo;
int foo = 1;

In this case, the overall definition is equivalent to:
extern volatile int foo = 1;

In C++, there's no such thing as a tentative definition. There's a one
definition rule that says you have to define the variable exactly once
and that's it. There's almost certainly not going to be a way to
persuade a C++ compiler to accept your definitions as they stand right
now.

As to how to make things work: I'm pretty sure you're not going to be
able to persuade a C++ compiler to accept the source code as it stands,
so it's basically going to come down to whether you can find a tool to
generate what you need. Offhand I'm not sure whether it can do the job
or not, but the first one I'd look at would probably be Doxygen.
 
G

Greg Herlihy

The following program compiles just fine in C, but not in C++.  Can
anyone explain why?  I have a chunk of code that defines stuff like
this in headers (without the extern) that I can not easily change.

The C compiler recognizes the first foo and second foo as the same.
The C++ compiler not so much.

Is there a way to get this to compile in C++ without changing all the
headers?

int foo;
int foo=1;

int
main(void)
{
        return foo;

}

A simple change that would make the above program valid in both C and C
++ would be to add an "extern" to "foo's" initial declaration:

extern int foo;
int foo = 1;

int main()
{
...

}

Greg
 
J

Jerry Coffin

[ ... ]
Actually, at file scope in C:

int foo;

...is a tentative definition.

int foo = 1;

...is a complete definition, nothing tentative about it.

Oops -- you're quite right. My code was correct, but the description was
a bit off -- since the last included an initializer, it clearly was NOT
a tentative definition. I knew I should have reread that part of the
standard before I posted that...
 
O

Old Wolf

The following program compiles just fine in C, but not in C++.

Is there a way to get this to compile in C++ without changing all the
headers?

Why would you want to convert working
C code to C++ ?
 
J

Juha Nieminen

Old said:
Why would you want to convert working
C code to C++ ?

Good question, given that C code can usually be compiled as C code,
and the resulting object files linked into the final C++ program without
problems. Only 'extern "C"' needs to be used on the C++ side when
including the C header files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top