Extern or #Include?

V

vkp

Hello Experts,

I am learning C++ by reading books....I don't understand why 'extern'
is needed? If the only purpose of declaring a variable extern is to
tell compiler that it resides in another file, why not include that
file using #include "thatfile.h"?

Thanks
-vkp
 
V

Victor Bazarov

vkp said:
I am learning C++ by reading books....I don't understand why 'extern'
is needed? If the only purpose of declaring a variable extern is to
tell compiler that it resides in another file, why not include that
file using #include "thatfile.h"?

The problem becomes apparent when you include that header in more than
one translation unit (TU) which is what the header is usually created
for. Unless your variable is a const (not going to change, no problem
if duplicated in every TU), you will run into the multiple definition error.

V
 
V

vkp

The problem becomes apparent when you include that header in more than
one translation unit (TU) which is what the header is usually created
for.  Unless your variable is a const (not going to change, no problem
if duplicated in every TU), you will run into the multiple definition error.

V

Thanks V, but could you please elaborate? So is there a limit on how
many times a file can be included in other files? why would it say
multiple definition, we are just including the file, we are not
redefining the variables of included file. As I said I'm beginner and
am curious to know this.
-vkp.
 
S

SG

Hello Experts,

I am learning C++ by reading books....I don't understand why 'extern'
is needed? If the only purpose of declaring a variable extern is to
tell compiler that it resides in another file, why not include that
file using #include "thatfile.h"?

An #include is nothing special. It's simply a text replacement where
#include "thatfile.h" is replaced by the contet of "thatfile.h". The
fact that you include some code doesn't change how declarations and
definitions are handled.

int foo; // definition
extern int xxx = 23; // definition
extern int bar; // declaration

(regardless of where these lines are actually stored)

Another important aspect of extern is that you can override linkage.
Some things have external linkage by default, others have internal
linkage by default. With "extern" you can force external linkage:

const double pi = 3.14159265; // definition, "pi" has internal
linkage
// because the object is const

extern const double e; // declaration, external linkage

You can place the above lines in header files without problems.
Exactly one of the translation units should however contain this line

extern const double e = 2.718281828; // definition, external

Check your C++ book on what linkage and ODR (one definition rule) is
about.


Cheers!
SG
 
R

Rolf Magnus

vkp said:
Hello Experts,

I am learning C++ by reading books....I don't understand why 'extern'
is needed? If the only purpose of declaring a variable extern is to
tell compiler that it resides in another file, why not include that
file using #include "thatfile.h"?

Indeed, why not? However, that header then contains the extern declarations.
 
B

Bill

Indeed, why not? However, that header then contains the extern declarations.

Why do I want to delcare them as extern in header file....the purpose
of using #include was to not use extern at all. Say a variable 'abc'
is in thatfile.h so if I want to use it in another file 'new.cpp', I
include thatfile.h without declaring abc extern.
-vkp
 
A

Alf P. Steinbach

* vkp:
Thanks V, but could you please elaborate? So is there a limit on how
many times a file can be included in other files? why would it say
multiple definition, we are just including the file, we are not
redefining the variables of included file. As I said I'm beginner and
am curious to know this.

When you have two files [a.cpp] and [b.cpp] and compile them separately, each
file's code may, for example, use an ordinary[1] variable called 'x', declared
outside any routine or class.

If 'x' has internal linkage (as it has with keyword 'static', and also by
default if it's 'const') then the 'x' in [a.cpp] is a different variable than
the one in [b.cpp].

If 'x' has external linkage then both names, in [a.cpp] and [b.cpp], refer to
the same variable, and must be declared with the same type.

And then, for the compiler (or more precisely the linker), there is the problem
of deciding whether e.g. the declaration of 'x' in [b.cpp] is an error, an
inadvertent use of the same name as in [a.cpp], or whether it's intentional,
meant to refer to the same variable as in [a.cpp].

Different languages solve that problem in different ways, and indeed C and C++
solve it in different ways.

In C++ there can and must be only 1 actual *definition* of 'x', e.g. in [a.cpp],
and the other declarations, e.g. in [b.cpp], must be pure declarations,
declarations that don't define the variable but just refer to it. When you don't
use 'extern' you have a definition, and also when you provide an explicit
initialization you have a definition. When you use 'extern' and don't provide
any explicit initialization, you have a pure declaration.


Cheers & hth.,

- Alf
 
A

Alan Woodland

vkp said:
Thanks V, but could you please elaborate? So is there a limit on how
many times a file can be included in other files? why would it say
multiple definition, we are just including the file, we are not
redefining the variables of included file. As I said I'm beginner and
am curious to know this.

Think of #include as like a copy and paste performed for you by the
compiler. Think about it this way - if you were allowed multiple
definitions which one should be used where in the code? What about
multiple definitions in the same TU?

Alan
 
R

Richard Herring

In message
Why do I want to delcare them as extern in header file....the purpose
of using #include was to not use extern at all. Say a variable 'abc'
is in thatfile.h so if I want to use it in another file 'new.cpp', I
include thatfile.h without declaring abc extern.

The variable is not "in" thatfile.h. What's in thatfile.h is either a
declaration of the variable or a definition of it (or maybe both ;-)
thatfile.h is just a sequence of characters which the compiler happens
to read because of a #include directive in some other file. It may get
read many times, during the course of compiling many compilation units,
so it makes no sense to think of it as any kind of unique place that
variables can be "in"..

If the variable is "in" anything, it's the place in the compiler's
output where space is reserved for storing it. And that will be the
output of the compiler for whichever translation unit the compiler was
processing when it met a *definition* (as opposed to a declaration) of
the variable.

The whole point of "extern" here is to ensure that what follows is *not*
a definition.
 
R

Rolf Magnus

Bill said:
Why do I want to delcare them as extern in header file....

Just for the same reason I would declare it extern outside of it.
the purpose of using #include was to not use extern at all.

No. The purpose of using #include is that you don't need to repeat the
extern declaration in every implementation file you want to use the variable
in. Instead you put that declaration in the header and #include that
header.
Say a variable 'abc' is in thatfile.h so if I want to use it in another
file 'new.cpp', I include thatfile.h without declaring abc extern.

And when you #include it in another .cpp file you get a linker error because
you have multiple definitions of your variable.
Including a header in a file does the same as copying the header's content
to that file. In fact, that's what the preprocessor actually does when it
sees an #include directive.
 
J

Juha Nieminen

Bill said:
Why do I want to delcare them as extern in header file....the purpose
of using #include was to not use extern at all. Say a variable 'abc'
is in thatfile.h so if I want to use it in another file 'new.cpp', I
include thatfile.h without declaring abc extern.

If you have a variable named abc declared in a header file, and the
declaration is not 'extern', then if you include that header file in
more than one source file, that variable will end up being instantiated
in more than one object file, and then the linker will give you an error
message because it sees more than one instance of that variable and has
no way of knowing which one is the "correct" one.

When you declare the variable as being 'extern', then the compiler
will *not* instantiate the variable in that object file. Then you can
explicitly instantiate it in only one place and the linker will be happy.

The same applies to function declarations, but the difference is that
function declarations are implicitly 'extern', so you don't have to
specify it explicitly. (But you can if you want. Try it.)
 

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,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top