By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

header files revisited: extern vs. static

P: n/a
Hello,

The extern keyword can be used in C and C++
to share global variables between files by
declaring the variable in header file and
defining it in only one of the two files.
For example, if x were not declared
extern in the header file below then
the linker would complain about x
being multiply defined.

--------------------
// main.cpp

#include "hello.h"

int x;

int main() {

x = 0;

hello();

}
--------------------
// hello.h

#ifndef HELLO_H
#define HELLO_H

extern int x;

void hello();

#endif // HELLO_H
--------------------
// hello.cpp

#include <iostream>
#include "hello.h"

void hello() {

std::cout << x << std::endl;

}
----------------------------------
output: 0
----------------------------------

However, I don't quite understand how
using the keyword static in the header
file allows us to define the variable
in the header file and include the
header file in multiple files.
After all, both global variables
and static variables are placed
in the static data segment of a
computer program when loaded
into memory so what is the
difference? Also, why is
1 still being printed in
the second case instead
of 0?

--------------------
// main.cpp

#include "hello.h"

int main() {

x = 0;

hello();

}
--------------------
// hello.h

#ifndef HELLO_H
#define HELLO_H

static int x = 1;

void hello();

#endif // HELLO_H
--------------------
// hello.cpp

#include <iostream>
#include "hello.h"

void hello() {

std::cout << x << std::endl;

}
----------------------------------
output: 1
----------------------------------

Thanks,

JG

Oct 30 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
John Goche wrote:
<snip>
However, I don't quite understand how
using the keyword static in the header
file allows us to define the variable
in the header file and include the
header file in multiple files.
After all, both global variables
and static variables are placed
in the static data segment of a
computer program when loaded
into memory so what is the
difference? Also, why is
1 still being printed in
the second case instead
of 0?
The keyword 'static' declares an object with internal linkage. The
#include just copies and pastes the contents of the header into the
source code. This means you've just defined TWO different objects with
internal linkage - one in each of your translation units. Probably not
what you intended, is it?

Regards,
Bart.

Oct 30 '06 #2

P: n/a
John Goche wrote:
However, I don't quite understand how
using the keyword static in the header
file allows us to define the variable
in the header file and include the
header file in multiple files.
After all, both global variables
and static variables are placed
in the static data segment of a
computer program when loaded
into memory so what is the
difference?
Non static global variables have external linkage, the linker can identify
the variables by his name and resolve his usage from different object
files. The static ones are internal, his names are not exported and can be
used only from the same object file. If you have two static variables with
the same name in different translations units, they are two different
variables. The result is the same if you put it in a header file or in each
of the cpp that include that header, header files are just like some way of
automated copy&paste.

A compiler can use a different build model that the supposed in this
explanation, but the result must be equivalent.

--
Salu2
Oct 30 '06 #3

P: n/a
Julián Albo wrote:
[..]
Non static global variables have external linkage, [..]
Nit-pick: .. unless they are declared 'const'.
Oct 30 '06 #4

P: n/a
Bart <ba***********@gmail.comwrote:
>The keyword 'static' declares an object with internal linkage. The
#include just copies and pastes the contents of the header into the
source code. This means you've just defined TWO different objects with
internal linkage - one in each of your translation units. Probably not
what you intended, is it?
What does the phrase "internal linkage" mean here? It almost
sounds like an oxymoron. What is being linked to what? Does
the linker need to manipulate such a static variable?

Steve
Oct 30 '06 #5

P: n/a
On Mon, 30 Oct 2006 21:33:40 +0000 (UTC), sp*****@speedymail.org
(Steve Pope) wrote in comp.lang.c++:
Bart <ba***********@gmail.comwrote:
The keyword 'static' declares an object with internal linkage. The
#include just copies and pastes the contents of the header into the
source code. This means you've just defined TWO different objects with
internal linkage - one in each of your translation units. Probably not
what you intended, is it?

What does the phrase "internal linkage" mean here? It almost
sounds like an oxymoron. What is being linked to what? Does
the linker need to manipulate such a static variable?

Steve
Every identifier in a C++ program has several attributes, and linkage
is one of them. There are exactly three types of linkage, external,
internal, and no linkage.

Internal linkage is defined in the C++ standard like this: "When a
name has internal linkage, the entity it denotes can be referred to by
name from other scopes in the same translation unit."

This differs from identifiers defined at block scope, which cannot be
referred to by name outside of the scope in which it was defined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 31 '06 #6

P: n/a
Jack Klein <ja*******@spamcop.netwrote:
>(Steve Pope) wrote in comp.lang.c++:
>What does the phrase "internal linkage" mean here? It almost
sounds like an oxymoron. What is being linked to what? Does
the linker need to manipulate such a static variable?
>Every identifier in a C++ program has several attributes, and linkage
is one of them. There are exactly three types of linkage, external,
internal, and no linkage.
>Internal linkage is defined in the C++ standard like this: "When a
name has internal linkage, the entity it denotes can be referred to by
name from other scopes in the same translation unit."
>This differs from identifiers defined at block scope, which cannot be
referred to by name outside of the scope in which it was defined.
Thanks. I find the choice of words -- "internal linkage" --
un-intuitive, relative to say "local to one source file", but
that's of no consequence here. ;)

Steve
of little
Oct 31 '06 #7

P: n/a
Steve Pope wrote:
[..] I find the choice of words -- "internal linkage" --
un-intuitive, relative to say "local to one source file", but
that's of no consequence here. ;)
It's unintuitive for a newcomer who never dealt with the table
of *external* symbols for a module (no offence intended). What
is the opposite of 'external'? See?

'External' has unfortunately two meanings. And I don't think
there is a way to overcome that. OOH, it's a symbol that cannot
be resolved locally, and as such is deemed to come from outside.
OTOH it's a symbol visible from outside, a symbol to which other
modules can refer (thus for other modules it also is 'external')
by name. 'Internal' is basically the opposite of both of those
meanings, i.e. defined inside and referred inside [the module].

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '06 #8

P: n/a
Geo

Victor Bazarov wrote:
Julián Albo wrote:
[..]
Non static global variables have external linkage, [..]

Nit-pick: .. unless they are declared 'const'.
If they are 'const', are they 'variables' !

Oct 31 '06 #9

P: n/a
Geo wrote:
Victor Bazarov wrote:
Julián Albo wrote:
[..]
Non static global variables have external linkage, [..]
Nit-pick: .. unless they are declared 'const'.

If they are 'const', are they 'variables' !
Yes. Technically, the C++ standard defines a 'variable' as being
"introduced by the declaration of an object". That's different than the
mathematical meaning of a 'variable'.

Regards,
Bart.

Oct 31 '06 #10

P: n/a
Victor Bazarov <v.********@comAcast.netwrote:
>Steve Pope wrote:
>[..] I find the choice of words -- "internal linkage" --
un-intuitive, relative to say "local to one source file", but
that's of no consequence here. ;)
>It's unintuitive for a newcomer who never dealt with the table
of *external* symbols for a module (no offence intended). What
is the opposite of 'external'? See?
>'External' has unfortunately two meanings. And I don't think
there is a way to overcome that. OOH, it's a symbol that cannot
be resolved locally, and as such is deemed to come from outside.
OTOH it's a symbol visible from outside, a symbol to which other
modules can refer (thus for other modules it also is 'external')
by name. 'Internal' is basically the opposite of both of those
meanings, i.e. defined inside and referred inside [the module].
The part of the phrase I find unintuitive is the word "linkage",
because I see no linking going on as a result of internal linkage.

The internal vs. external distinction I have no problem with.

Steve
Oct 31 '06 #11

P: n/a
Steve Pope wrote:
Victor Bazarov <v.********@comAcast.netwrote:
>Steve Pope wrote:
>>[..] I find the choice of words -- "internal linkage" --
un-intuitive, relative to say "local to one source file", but
that's of no consequence here. ;)
>It's unintuitive for a newcomer who never dealt with the table
of *external* symbols for a module (no offence intended). What
is the opposite of 'external'? See?
>'External' has unfortunately two meanings. And I don't think
there is a way to overcome that. OOH, it's a symbol that cannot
be resolved locally, and as such is deemed to come from outside.
OTOH it's a symbol visible from outside, a symbol to which other
modules can refer (thus for other modules it also is 'external')
by name. 'Internal' is basically the opposite of both of those
meanings, i.e. defined inside and referred inside [the module].

The part of the phrase I find unintuitive is the word "linkage",
because I see no linking going on as a result of internal linkage.

The internal vs. external distinction I have no problem with.
Well, 'linkage' is probably an awkward short replacement to 'symbol
resolution'.

Besides, if you agree to use 'linkage' for 'external', what would
you use for 'internal'? And if we agree that those two definitions
are opposites of each other, why would we use different nouns if
we already use different adjectives? What's the opposite of, say,
'powerful kick'? 'Weak nudge'? 'Weak push'? :-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '06 #12

P: n/a
Steve Pope wrote:
<snip>
The part of the phrase I find unintuitive is the word "linkage",
because I see no linking going on as a result of internal linkage.
Perhaps that's because you're used to thinking of 'linking' as the
process of putting together different translation units. If instead you
think of 'linking' as the process of putting together function and
object definitions it may become clearer. Or if you prefer, the linker
links the points of reference to the point of definition.

Regards,
Bart.

Oct 31 '06 #13

P: n/a
Jack Klein wrote:
Every identifier in a C++ program has several attributes, and linkage
is one of them. There are exactly three types of linkage, external,
internal, and no linkage.
In addition, and this may add to the confusion, identifiers also have a
'language linkage'. This may be changed by adding a 'linkage
specification' such as extern "C", which is a way to link to
identifiers specified in other languages.
Regards,
Bart.

Oct 31 '06 #14

P: n/a
Bart <ba***********@gmail.comwrote:
>Steve Pope wrote:
>The part of the phrase I find unintuitive is the word "linkage",
because I see no linking going on as a result of internal linkage.
>Perhaps that's because you're used to thinking of 'linking' as the
process of putting together different translation units.
Yes, that's exactly what I think of "linking" as.
If instead you think of 'linking' as the process of putting
together function and object definitions it may become
clearer. Or if you prefer, the linker links the points of
reference to the point of definition.
I'll go along with the last sentence. Thanks.

Steve
Nov 1 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.