473,395 Members | 1,452 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,395 software developers and data experts.

header files revisited: extern vs. static

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
14 13395
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
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
Julián Albo wrote:
[..]
Non static global variables have external linkage, [..]
Nit-pick: .. unless they are declared 'const'.
Oct 30 '06 #4
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how...
6
by: Ravi | last post by:
Hi All: Is there any reason for declaring functions as static in a header file if that header file is going to be included in several other files? The compiler throws a warning for every such...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
4
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in...
3
by: John Goche | last post by:
A common C++ trend is to use const int foo = 10; where some C programmers would have used #define FOO 10 in order to avoid preprocessor overheads.
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
16
by: wdh3rd | last post by:
Hi everyone. I'm new to C and I have a few questions: I am making files for permutations and combinations. Files to be made are perm.c, perm.h, combo.c, and combo.h. Since both combinations...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.