Re: question about extern ?..

From: JKop (NULL_at_NULL.NULL)
Date: 06/08/04


Date: Tue, 08 Jun 2004 15:44:40 GMT

mark posted:

> compiler g++ on linux
>
> file.h has the following, int x=5;
> and inside the main program file I can access "x" without declaring it
> "extern x". Program compiles and runs fine....
>
> also program compiles and runs fine with x declared as extern.
>
> So is it necessary to declare x as extern if one get away without
> declaring it extern ? What is the standard and good practice ?

First of all, let's say you have a CPP file as so:

#include "blah.hpp"

int main(void)
{
            some_variable = 7;
}

After the preprocessor has processed that, you'll have a translation unit:

extern int some_variable;

int main(void)
{
            some_variable = 7;
}

Now, the thing here is that no variable is declared at all!! The "extern"
says "Don't actually make a variable. There's already one made somewhere
else in some other CPP file. Compile *ME*, _this_ CPP file, and then let the
linker bother with finding out where the variable was actually declared!"

So our CPP file gets compiled. Now, somewhere else, you _MUST_ have CPP file
containing:

int some_variable;

So now, the linker takes all your translation units, which the compiler has
turned into object files, and sorts it all out.

If you DON'T want a particular variable to be visible outside of your CPP
file, then:

static int some_variable;

In this case, you won't get a compile error at all, but a Link errror, the
linker can't find the variable to which your "extern" statement refers.

-JKop



Relevant Pages

  • Re: MIDL file query
    ... MIDL just generates the code to declare an MIDL_TYPE_FORMAT_STRING object with external visible, then use the "static" define it. ... The "extern" declares the static storage class object, so there is no problem using the "static" to define it. ... No problem except that for historical reasons I need to compile the file using a non-MS compiler, ... At this stage, I can compile and run the output from MIDL V2, but although I can get the output from MIDL V6 to compile by changing static to extern, as yet have been unable to get it to run without crashing - I think in the RpcServerListen routine the first time a call is made from the client. ...
    (microsoft.public.vc.language)
  • Re: Mixing C and CPP files.
    ... Victor Bazarov wrote: ... it, I can keep the C files, but I have to add one CPP file. ... Just make sure that the headers have conditional 'extern "C"' modifiers in them, ... Perhaps the answer to the OP's question is that Visual Studio will compile as a C program any file with the extension ".c" and will compile ...
    (microsoft.public.vc.language)
  • Re: extern for global variable: C Question
    ... David Wilkinson wrote: ... declare each variable using extern in some .h file ... Have the VAR_DEFINITION defined in one .cpp file among all that include the .h file. ...
    (microsoft.public.vc.language)
  • Re: Problem Linking (win32)
    ... version of nasm and the latest version of golink. ... extern MessageBoxA ... your application to compile, link and run. ... A consistent error occurs upon the loading of my asm. ...
    (comp.lang.asm.x86)
  • Re: Understanding # and function variables
    ... I wanted to stress it for two reasons. ... will assume you meant to declare it special, ... you establish some bindings at compile ...
    (comp.lang.lisp)