extern sample in MSDN
-
Hello everyone, I think in practical experience, extern is useful only when we have more than one compile unit (cpp file) and making cross-reference between compile units. In the MSDN extern sample, http://msdn2.microsoft.com/en-us/library/0603949d.aspx It only uses one source file (compile unit) to demonstrate the usage of extern, is it correct and practical? thanks in advance, George
-
Hello everyone, I think in practical experience, extern is useful only when we have more than one compile unit (cpp file) and making cross-reference between compile units. In the MSDN extern sample, http://msdn2.microsoft.com/en-us/library/0603949d.aspx It only uses one source file (compile unit) to demonstrate the usage of extern, is it correct and practical? thanks in advance, George
-
It only shows the its usage. U'rs thinking is absolutely correct.
Come online at:- jubinc@skype
Hi Don, I also think it is correct. But do we achieve special benefits if we use extern in the same compile unit? regards, George
-
Hi Don, I also think it is correct. But do we achieve special benefits if we use extern in the same compile unit? regards, George
No. The reason for
extern
is to differentiate between a declaration and a definition:int num; // Is this a definition or a declaration?
In this case the compiler assumes it’s a definition. If you want to sharenum
between two compilation units this will not do the trick and will result in a linker error – there will be two variables with the same name.extern int num; // This is a declaration.
Now this referrers to a “num” defined elsewhere.Steve
-
No. The reason for
extern
is to differentiate between a declaration and a definition:int num; // Is this a definition or a declaration?
In this case the compiler assumes it’s a definition. If you want to sharenum
between two compilation units this will not do the trick and will result in a linker error – there will be two variables with the same name.extern int num; // This is a declaration.
Now this referrers to a “num” defined elsewhere.Steve
Thanks Steve, In the sample, even if you do not use why we need to write statement, extern int i? i is global and we can use it anywhere in the same compile unit (cpp file). regards, George
-
Thanks Steve, In the sample, even if you do not use why we need to write statement, extern int i? i is global and we can use it anywhere in the same compile unit (cpp file). regards, George
As I said,
extern
is used to indicate that a construct is a declaration and not a definition. With functions no such mechanism is needed: if the function has a body it’s a definition and if not it’s a declaration:int void FooBar(); // This is a declaration (no body).
int void FooBar() // This is a definition (has body).
{
// Do stuff…
}If you want to use the “FooBar” function in another compilation unit (from where it’s defined) you need to make sure to include its declaration. This is usually done by including a header file. It’s an error to define the same function twice in two separate compilation units however and attempting to do so will result in a link error. Since variables don’t have bodies this technique isn’t usable, thus the
extern
keyword:int FooBar; // This is a definition.
extern int FooBar; // This is a declaration.Steve
-
As I said,
extern
is used to indicate that a construct is a declaration and not a definition. With functions no such mechanism is needed: if the function has a body it’s a definition and if not it’s a declaration:int void FooBar(); // This is a declaration (no body).
int void FooBar() // This is a definition (has body).
{
// Do stuff…
}If you want to use the “FooBar” function in another compilation unit (from where it’s defined) you need to make sure to include its declaration. This is usually done by including a header file. It’s an error to define the same function twice in two separate compilation units however and attempting to do so will result in a link error. Since variables don’t have bodies this technique isn’t usable, thus the
extern
keyword:int FooBar; // This is a definition.
extern int FooBar; // This is a declaration.Steve
Thanks Steve, How about this, extern int i = 100; a definition or declaration, why? regards, George
-
Thanks Steve, How about this, extern int i = 100; a definition or declaration, why? regards, George
From section 3.1.2 of the C++ standard: “A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (7.5) and neither an initializer nor a function-body, it declares a static data member in a class declaration (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), or a using-directive (7.3.4).”
Steve
-
From section 3.1.2 of the C++ standard: “A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (7.5) and neither an initializer nor a function-body, it declares a static data member in a class declaration (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), or a using-directive (7.3.4).”
Steve
Thanks Steve, 1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right? 2. What is " linkage-specification (7.5)"? regards, George
-
Hello everyone, I think in practical experience, extern is useful only when we have more than one compile unit (cpp file) and making cross-reference between compile units. In the MSDN extern sample, http://msdn2.microsoft.com/en-us/library/0603949d.aspx It only uses one source file (compile unit) to demonstrate the usage of extern, is it correct and practical? thanks in advance, George
-
Thanks Hamid, But it does not cover the case when using extern to qualify a variable which is defined in the same compile unit. :-) regards, George
-
Thanks Steve, 1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right? 2. What is " linkage-specification (7.5)"? regards, George
George_George wrote:
1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right?
No, because of the "and neither an initializer nor a function-body" clause; the example you gave does have an initializer.
extern int i; // This is a declaration.
extern int i = 100; // This contains an initializer and thus is a definition (and a declaration).George_George wrote:
2. What is " linkage-specification (7.5)"?
extern "C"
is an example.Steve
-
Thanks Hamid, But it does not cover the case when using extern to qualify a variable which is defined in the same compile unit. :-) regards, George
Doing so is poinless and possibly misleading, but harmless.
Steve
-
George_George wrote:
1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right?
No, because of the "and neither an initializer nor a function-body" clause; the example you gave does have an initializer.
extern int i; // This is a declaration.
extern int i = 100; // This contains an initializer and thus is a definition (and a declaration).George_George wrote:
2. What is " linkage-specification (7.5)"?
extern "C"
is an example.Steve
Thanks Stephen, Any special advantage/restrictions/functions we could have when writing extern int i = 3 other than int i = 3? (I think you mean extern int i = 3 has the same meaning as int i = 3, which is definition with initialization. right?) regards, George
-
Thanks Stephen, Any special advantage/restrictions/functions we could have when writing extern int i = 3 other than int i = 3? (I think you mean extern int i = 3 has the same meaning as int i = 3, which is definition with initialization. right?) regards, George
There is no reason to do so (use
extern
) in such cases. Doing so does neither harm nor good; the two constructs are equivalent. I wouldn’t in cases like that however as it could lead to confusion.Steve
-
There is no reason to do so (use
extern
) in such cases. Doing so does neither harm nor good; the two constructs are equivalent. I wouldn’t in cases like that however as it could lead to confusion.Steve
Thanks Steve, My question is answered. regards, George