473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

question about extern ?..

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 ?
Jul 22 '05 #1
10 1882
mark wrote:
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2
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)
Jul 22 '05 #3
On 8 Jun 2004 05:09:26 -0700, ma*********@yahoo.com (mark) wrote:
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.

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"
Jul 22 '05 #4
mark wrote:
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
Jul 22 '05 #5
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
Jul 22 '05 #6
Bill Seurer wrote:

mark wrote:
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


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
Jul 22 '05 #7
Default User wrote:
Bill Seurer wrote:
mark wrote:

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

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.

--
Regards,
Buster.
Jul 22 '05 #8
Buster wrote:
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.


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

Brian Rodenborn
Jul 22 '05 #9
Default User wrote:
Buster wrote:
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.


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.
Jul 22 '05 #10
Bill Seurer wrote:

Default User wrote:

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.

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
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Vijay Kumar R Zanvar | last post by:
Hi, Which section of C99 says that return value of malloc(3) should not be casted? Thanks. -- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
7
by: Kevin | last post by:
Hi al I have an interesting question.... I am working witha Win API this is the Function Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal...
2
by: Al Bahr | last post by:
H I am try to convert C# to vb.net I don’t know what would be an equivalent statements in VB.net any help will be appreciated. bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat.....
0
by: Al Bahr | last post by:
H I am try to convert C# to vb.net I don’t know what would be an equivalent statements in VB.net any help will be appreciated bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat.. ...
2
by: JoeC | last post by:
I am trying to create a message box that allows you to enter a name then display that name on a window. The program uses win32 but that is not the issue. I want have a global buf variable. So...
5
by: jchludzinski | last post by:
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a single x (declared somewhere) which would global to both compilation units: w.c & ww.c. No matter where I place the "extern"...
17
by: Francine.Neary | last post by:
I have a program that uses a large lookup table, provided as a large array in the source: static int bigtbl={123, 456, /* etc. etc. */ 9999 }; Now this table is pretty big, and having it...
6
by: vaclavpich | last post by:
Hi, I have a question on constant variables. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.