question about extern ?..

M

mark

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 ?
 
A

Andrey Tarasevich

mark said:
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....

That's because your 'file.h' is '#include'd into exactly one translation
unit. If you try including it into more than one translation unit (and
that's pretty much what '.h' files are for), you'll end up with multiple
definitions of 'x'. Your program will no longer compile due to ODR
violation (usually reported at linking stage).
also program compiles and runs fine with x declared as extern.

What exactly do you mean? This

extern int x = 5;

? This is also a definition. In namespace scope this is the same as the
original

int x = 5;

All I said above still applies.
So is it necessary to declare x as extern if one get away without
declaring it extern ? What is the standard and good practice ?

The good practice is to put a non-defining declaration of 'x' into the
header file

extern int x; // Note: no initializer here

and put the definition into one (and only one) of the implementation files

int x = 5;

Of course, another good practice is to avoid global variables unless
they are really necessary.
 
M

mark

let me ask this simple question.

where does one put the "extern int x" statement ?
(a) In the header (.h) file
(b) or the program file (.cpp)
 
B

Bill Seurer

mark said:
let me ask this simple question.

where does one put the "extern int x" statement ?
(a) In the header (.h) file
yes

(b) or the program file (.cpp)

no
 
A

Andre Heinen

let me ask this simple question.

where does one put the "extern int x" statement ?
(a) In the header (.h) file
(b) or the program file (.cpp)

As Andrey said, put "extern int x;" in the .h file,
*and* "int x = 5;" in *one* of the .cpp files.
 
J

JKop

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
 
D

Default User

Bill said:

Why can't you put an extern declaration in a .cpp file? Why do you think
it's better in a header? Shouldn't a variable be declared at the closest
practical point to its usage?

Everybody skipped the best advice, don't use globals at all unless there
is an extremely good reason, and newbie programs rarely have such
reasons.




Brian Rodenborn
 
B

Buster

Default said:
Why can't you put an extern declaration in a .cpp file?

You can, but it's better in a header file.
Why do you think it's better in a header?

Because, since the variable is being used in more than one
translation unit (otherwise it needn't be extern), putting
the extern declaration in the translation units leads to
unnecessary code duplication and the maintenance problems
that go with that.
Shouldn't a variable be declared at the closest
practical point to its usage?
Sometimes.

Everybody skipped the best advice, don't use globals at all unless there
is an extremely good reason, and newbie programs rarely have such
reasons.

No, they didn't.
 
D

Default User

Buster said:
Because, since the variable is being used in more than one
translation unit (otherwise it needn't be extern), putting
the extern declaration in the translation units leads to
unnecessary code duplication and the maintenance problems
that go with that.

I still believe that it should be explicitly declared at the point of
usage.



Brian Rodenborn
 
B

Bill Seurer

Default said:
I still believe that it should be explicitly declared at the point of
usage.

That is sort of what people used to do when C first came out, "extern
X"s and function declarations were scattered throughout their code.
Guess what happens when someone changes the definition of one of them?

The declarations should all be kept in one spot.
 
D

Default User

Bill said:
Default User wrote:

That is sort of what people used to do when C first came out, "extern
X"s and function declarations were scattered throughout their code.
Guess what happens when someone changes the definition of one of them?

The declarations should all be kept in one spot.


If the definition of a global variable changes, you've usually got
bigger problems than just the declarations.

I don't like globals and don't like globally declared globals (if that
makes sense).



Brian Rodenborn
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top