Re: extern "C" and global variables
Re: extern "C" and global variables
- Subject: Re: extern "C" and global variables
- From: Rush Manbert <email@hidden>
- Date: Fri, 31 Mar 2006 17:18:33 -0800
Another round?
John Weeks wrote:
Rush:
Thanks for considering my problem.
John Weeks wrote:
Hi, folks.
I have a C++ source file that defines a global variable:
extern "C" unsigned char gSuppressLiveUpdate = 0;
so that it can be used from a C source file. In the C file, of course, I
have
extern unsigned char gSuppressLiveUpdate;
gcc complains (OK, it's just a warning):
warning: 'gSuppressLiveUpdate' initialized and declared 'extern'
I guess I see the point of the warning; it's not really extern because it
is being defined right there. But then, how do I prevent name mangling so
that it can be used in the C file, without using the "extern" keyword?
At the moment I have suppressed the warning by defining and initializing it
in the C file and using just extern "C" unsigned char gSuppressLiveUpdate;
in the C++ file. But that's not satisfying; I want to be able to put the
definition where I want it.
CodeWarrior and VC++.net are both OK with this, but I realize that gcc is
actually more standards-compliant that they are. I tried searching for an
answer with Google, but all the discussions are about functions declared
extern "C".
It would be "more standard" to move the declaration to the corresponding
header file, i.e.
extern "C" unsigned char gSuppressLiveUpdate;
and keep the definition in the C++ file, i.e.
unsigned char gSuppressLiveUpdate = 0;
This should take away the warning, and shouldn't have any effect on the
C file, since it's doing its own declaration. (But it should read a
header that contains this declaration. Really it should read the same
header as the C++ file. Might require some #ifdef hanky panky, or just a
globals.h file that's included both places.)
- Rush
I agree that the use of header files here is not up to snuff wrt usual
practice.
What you are saying is essentially the same as (except for re-organizing
the header files):
extern "C" unsigned char gSuppressLiveUpdate;
unsigned char gSuppressLiveUpdate = 0;
and, in fact, that fixes the problem. Looks odd, though.
I guess the confusion comes from the fact that C++ is using "extern" to
mean something slightly different from what C means. But it's only
different when it is extern "C"; C++ uses extern just like C otherwise!
Hi John,
I would take a different view. The purpose of the extern keyword, at
least this is what I have always thought, is to identify variables and
functions that are implemented in a different compilation unit, as in
"the unsigned char called gSuppressLiveUpdate is an external reference."
In C++, it also has a role in determining the mangled name of functions
and variables.
Since most code declares the externs in header files, and the header
file gets included by the compilation unit that defines the variable,
having the declaration and definition in the same file is allowed. This
is the equivalent of your separated statements above.
However, doing this in a C++ file:
extern "C" unsigned char gSuppressLiveUpdate;
unsigned char gSuppressLiveUpdate = 0;
just defines a local variable called gSuppressLiveUpdate whose name
won't be C++ mangled, the extern has no other effect at all, and no
other files see it (unless you've done something sort of strange).
Likewise,
extern "C" unsigned char gSuppressLiveUpdate = 0;
in your C++ file only changes the name mangling. The extern has no other
effect. The scoping of the variable is controlled by other factors. This
is why the compiler warning makes sense to me, because it thinks you did
this by accident. (A pretty anthropomorphic viewpoint of the compiler, I
know. :-)
Regards,
Rush
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden