This is from the <iostream>:
namespace std
{
extern istream cin; ///< Linked to standard input
extern ostream cout;
...
It seems by using extern the data types defined in other namespaces will just be available?
extern means "these variables are defined in some other compilation unit (.cpp or .lib file)"
In this case, you #include <iostream> into your .cpp file, and because cin and cout are declared as extern, the compiler will let you use them without complaining. Then, when the linker runs, it looks up all of the extern variables and sorts it all out.
extern is used to refer to a variable defined in a different compilation unit (for now, you can think of a compilation unit as a .cpp file). The statements in your example declare rather than define cin and cout. It is telling the compiler that the definition of these objects is found in another compilation unit (where they are not declared as extern).
No, this is an explicit way to say cin and cout are declared without actually defining them.
The extern keyword tells the compiler that a variable is declared in another source(i.e outside the current scope). The linker then finds this actual declaration and sets up the extern variable to point to the correct location.
variables declared by extern statements will not have any space allocated for them, as they should be properly defined elsewhere. If a variable is declared extern, and the linker finds no actual declaration of it, it will show error.
Eg. extern int i;
//this declares that there is a variable named i of type int, defined somewhere in the program.
Some answers say here that extern means that variable is defined in other compilation unit. Then, following should not compile as no other compilation unit or file is provided to compiler.
extern int a;
int main(){
std::cout<<a<<std::endl; //Prints 3
}
int a=3;
So, I think extern is explicitly used to separate the declaration and definition in case of variable as stated in one of answer. I think it is useless in case of functions.
Related
Due to some static data, I have a function
void foo(MyNamespace::bar)
defined in a certain compilation unit. But its point of use is in another compilation unit. So I use
namespace MyNamespace
{
extern void foo(bar);
}
But the linker can't find the function definition. Am I misusing extern?
extern can be used for this kind of thing.
Your problem is that the linker is expecting a function MyNamespace::foo(bar); due to the fact that your extern statement is within MyNamespace.
You have two choices:
use extern void foo(MyNamespace::bar); at your "point of use". Don't enclose that line within MyNamespace.
Alternatively, enclose the function definition within MyNamespace.
extern is a storage class in C. How exactly does it work? The output of the code given below is 20. How is this the output?
#include <stdio.h>
int main()
{
extern int a;
printf("%d", a);
return 0;
}
int a=20;
It means three things:
The variable has external linkage, and is accessible from anywhere in the program;
It has static storage duration, so its lifetime is that of the program (more or less); and
The declaration is just a declaration, not a definition. The variable must also be defined somewhere (either without the extern, or with an initialiser, or in your case, both).
Specifically, your extern int a; declares that the variable exists, but doesn't define it at that point. At this point, you can use it, and the linker will make sure your use refers to the definition. Then you have the required definition, int a=20; at the end, so all is well.
extern in this case indicates that the symbol a is defined in a different location, such as a different module. So the linker looks for a symbol with the same name in all of the modules that are linked, and if one exists then it sets the address to your local variable a with the address to the externally defined variable. Since you have another a defined outside of your main() function, the a inside your main() function is (basically) the same variable as the one outside.
Since the global a is initialized before the main function executes, the value is 20 by the time you access it.
extern means i declare a variable, just like you implement a function in a source file and declare the prototype in a header to allow other source file to use it.
If you put a global variable in a source file, and use a header to declare it with the extern keyword, each source file including the header will see the variable.
The linker will do the job to tie everything just as it does with functions
extern as a storage class specifier tells the compiler that the object being declared is not a new object, but has storage elsewhere, i.e., is defined elsewhere. You can try this experiment with your code to see how it works. Leave out the keyword extern in your declaration of int a in main(). Then your printf() will print some garbage value, as it would be a new definition of an int with the same identifier, which would hide the global a declared elsewhere.
You use extern to tell the compiler that the variable is defined elsewhere. Without extern in your program compiler would define another variable a (in addition to this in the global scope) in your main() function that would be printed uninitialized.
There is one variable called BOT_TIME that varies with the difficulty of my game, and hence isn't const. There are many files that use it. I intend to use it as a global variable.
1) In constants.h I declare it extern int BOT_TIME.
In constants.cpp, I declare it extern int BOT_TIME.
BUILD => undefined references to the variable in all sources(Yes, I've included the header).
2) In constants.h I declare it int BOT_TIME.
In constants.cpp, I declare it int BOT_TIME.
Since non-consts are by default extern , I decided to leave that keyword.
BUILD => Multiple definition of the variable (shows in each source file that has constants.h included)
3) In constants.h I declare it extern int BOT_TIME.
In constants.cpp, I declare it int BOT_TIME.
This works.
Where is the issue?
Initializing the variable to something in constants.cpp makes it work for cases 1 and 3.
What is this happening?
Which is the right approach?
You can declare a variable as many times as you want, you can and have to define it only once.
extern int BOT_TIME;
is a declaration.
int BOT_TIME;
is a definition.
The definition has to appear in a single implementation file.
Since non-consts are by default extern , I decided to leave that keyword.
Nope. How'd you figure?
To answer the questions:
1) There's no definition, only declarations.
2) The assumption is wrong. You define the symbol multiple times.
3) It works because that's the correct way to do it.
From the comments:
When you declare a variable with extern, you specify that it has external linkage, yes, but you only declare, and not define it. If you leave it without the extern keyword, it still has external linkage, but it's also a definition.
You need to declare the variable in a header file and define it once and only once in a source file.
The correct way to do this is:
constants.h
//declare the symbol as extern
extern int BOT_TIME;
constants.cpp
#include "constants.h"
//define the symbol once and only once
int BOT_TIME;
XXXX.cpp
#include "constants.h"
//Include the header file which declares it extern in any source file
//you want to access it
//use BOT_TIME
You can declare a variable as many times but you can only define it once.
If you define a variable more than once you violate the One Definition Rule.
extern int BOT_TIME;
is a declaration and #1 only makes the same declaration twice, this is allowed but it doesn't work since you never define the variable.Note that every extern variable must be defined once or the compiler cannot find its definition and hence complains.
In #2 you define the same symbol multiple times in different translation units.This violates the One Definition rule and hence the linker reports the same.
#3 is the correct way to do it.
Good Read:
What is the difference between a definition and a declaration?
extern int x; // declaration
int x; // definition
extern int x = 3; // definition
In general, you need a declaration in the header file, for source files that need to know about x. You need one definition, in one source file, so that there is actually an object x.
Well, reading "a bit old" book ("The C programming language", second edition, by Dennis Ritchie), I came a cross the following:
An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it
and I was like - what?!
"The variable must also be declared in each function that wants to access it". Then, I was shocked one more time:
int max;
/* ... */
int main()
{
extern int max;
/* ... */
}
And one more - what?!
As far as I know (obviously, it's not much and far from enough), extern makes sense only when you define a global variable somewhere and you want to access it through another file (not to define it again).
So:
What's the point of this extern int max inside the main or any other function?
Does the standard really says, that this is a must (that I need to declare, for this example, this max in each function, that will use it?)
Is this the same for C++ (that's why I placed the C++ tag)? This is the first time I see something like this.
Note: this is not the same as What is the use of declaring a static variable as extern inside a function?
Your post surprised me. I had no recollection of that and I've read K&R long ago. I only have the first edition here and it is there too. However, that is not all it says. From the first edition:
The variable must also be declared in each function that wants to
access it; this may be done either by an explicit extern declaration
or implicitly by context.
Note the "implicitly by context." Later in the text:
...if the external definition of a variable occurs in the source file
before its use in a particular function, then there is no need for an
extern declaration in the function. The extern declarations in main,
... are thus redundant. In fact, common practice is to place
definitions of all external variables at the beginning of the source
file, and then omit all extern declarations.
So this is saying that making the extern variable visible can be done inside the function for just that function, or can be done outside any function for all functions following it in the source file. I believe that this is the only place in the book where it is done inside the function, later it uses the familiar once for the file approach.
extern int max inside main or function is saying to the compiler "I am not a local variable inside the main or function, I am the global variable defined elsewhere".
If the global is declared in the same file, not useful. In different file,yes, but not in each function, just declare one time in the head file of the source that use this global variable. This is the same in c++.
The extern is linkage. It means this name, max, is linked to other occurrences of the name, possibly in other files. (That is, when the object modules are linked together to make an executable, all the linked references to this name will be made to refer to the same object.)
The scope of this declaration is the remainder of the function body it is in. That means other functions in this file do not see the name declared by this declaration (unless they declare it themselves).
Scope and linkage are different things.
Another possible reason for extern inside a function is to make sure that no local variable is shadowing the global variable
Without extern:
int max = 33;
int main()
{
int max;
printf("%d", max); // prints indeterminate value (garbage)
}
With extern
int main()
{
extern int max;
int max;
printf("%d", max);
}
Output:
error: redeclaration of ‘max’ with no linkage
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is external linkage and internal linkage in C++
Actually I want to know the importance of extern.
First I wrote some code :
file1.h
extern int i;
file2.c
#include<stdio.h>
#include "file1.h"
int i=20;
int main()
{
printf("%d",i);
return 0;
}
Now my question is that: what is the use of extern variable when I have to define the i in file2.c, declaring in file1.h is in which way useful to me .
sudhanshu
extern allows you to declare a variable (notify the compiler of its existence) without defining it (bringing it into existence). The general rule is that you can declare things as many times as you want, but you can only define them once.
This is useful if, for example, it will be defined elsewhere.
Consider:
file1.c: file2.c:
extern int xyzzy; int xyzzy;
void fn (void) {
xyzzy = 42;
}
When you link together those two functions, there will be one xyzzy, the one defined in file2.c, and that's the one that fn will change.
Now that happens even without the extern. What the extern does is to declare that xyzzy exists to file1.c so that you don't get any I don't know what the blazes "xyzzy" is errors.
In your particular case (if those are your only two files), you don't need it. It's only needed if you had another file that #included file1.h and tried to access i.
It's useful when you have three files:
foo.h
extern int i;
void bar();
a.c:
#include "foo.h"
int i = 6;
int main(){
bar();
}
b.c:
#include "foo.h"
void bar(){
printf("%d\n", i);
}
As you can see, i is shared between both a.c and b.c, not redefined per-file, as would happen without the extern keyword.
The "extern" declaration in C is to indicate that there is an existence of, and the type of, a global variable.
In C mostly each .c file behaves like a seperate module. Hence a variable with "extern" is something that is defined externally to the current module.
It is always a better practise to define the global in one place, and then declare extern references to it in all the other places. When refering to globals provided by any shared library, this is important in order to make sure that your code is referring the correct and common instance of the variable.
From Wikipedia:
When a variable is defined, the compiler allocates memory for that
variable and possibly also initializes its contents to some value.
When a variable is declared, the compiler requires that the variable
be defined elsewhere. The declaration informs the compiler that a
variable by that name and type exists, but the compiler need not
allocate memory for it since it is allocated elsewhere.
Extern is a way to explicitly declare a variable, or to force a
declaration without a definition
If a variable is defined in file 1, in order to utilize the same
variable in another file, it must be declared. Regardless of the
number of files, this variable is only defined once, however, it must
be declared in any file outside of the one containing the definition.
If the program is in several source files, and a variable is defined
in file1 and used in file2 and file3, then extern declarations are
needed in file2 and file3 to connect the occurrences of the variable.
Having extern tells the compiler its a declaration of a variable.
In your example in file1.h imagine what would happen if you didn't specify extern keyword. It will look like there are two definitions of int i. Also, a header file can be included in many .c files. If you compile those .c files and link them, linker would see multiple definitions of the same variable.
extern keyword allows you to say to the compiler ... " Use this variable right now .. I will define and initialize it later ".. i.e " Compile it now .. I will link to its definition later".
You can extern the variable when you define it in some other file and you want to use it in the current context..
The declaration in the header allows you to access the variable from more than one source file, while still only defining it in one place.