Explanation of the extern keyword in C/C ++

Source: Internet
Author: User

Source: http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777431.html

1Basic explanation: Extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function. In addition, extern can be used to specify links.

That is to say, extern has two functions. First, when it is connected with "C", for example, extern "C" void fun (int a, int B ); the compiler will tell the compiler to translate the corresponding function name instead of the C ++ name according to the C rule when compiling the function name fun, when translating this function name, the C ++ rule will make the name "fun" completely invisible. It may be fun @ aBc_int_int # % $ or something else, this depends on the "temper" of the compiler (the methods used by different compilers are different). Why does this happen because C ++ supports function overloading, I will not discuss this issue too much here. If you are interested, you can search for it online. I believe you can get a satisfactory explanation!
Second, when extern is not modified with "C", such as in the header file: extern int g_Int; its function is to declare the keyword of the function or global variable's function scope. The declared function and variable can be used in other modules in this module. Remember that it is a declaration not a definition! That is to say, if Module B (compilation unit) references the global variables or functions defined in module A, it only needs to include the header file of module A. During the compilation phase, although Module B cannot find this function or variable, it does not report an error. It will find this function from the target code generated by module A during connection.

2. Problem:Extern variable
An array is defined in a source file: char a [6];
The following statement is used in another file: extern char *;
Can this be done?
Answer and analysis:
1) No. Illegal access will be reported during the program running. The reason is that the pointer to type T is not equivalent to the array of type T. Extern char * a declares a pointer variable instead of a character array. Therefore, it is different from the actual definition, resulting in invalid access during runtime. Change the statementExtern char a [].
2) The example analysis is as follows. If a [] = "abcd", the external variable a = 0x61626364 (the ASCII code value of abcd), * a is obviously meaningless.
Obviously, the space (0x61626364) that a points to is meaningless and prone to illegal memory access.
3) This reminds us that we must strictly match the declared format when using extern. In actual programming, such errors are not uncommon.
4) extern is often used in variable declaration *. the c file declares a global variable. If the global variable is to be referenced, it is placed in *. h and use extern to declare.

3. Problem:Modify the prototype of the extern function.
When the function provider unilaterally modifies the function prototype, if the user does not know to continue to use the original extern statement, the compiler will not report an error during compilation. However, during the running process, a system error is often caused by missing or missing input parameters. How can this problem be solved?
Answer and analysis:
Currently, the industry does not have a perfect solution to deal with this situation. The common practice is that the provider provides external interface declarations in its own xxx_pub.h, and then the caller includes the header file, in this way, the extern step is omitted. To avoid such errors.
Baojian has a dual front. For the application of extern, different practices should be selected for different occasions.

4. Questions:Extern "C"
When using C functions in the C ++ environment, the compiler often fails to find the C function definition in the obj module, resulting in a link failure. How can this problem be solved?

Answer and analysis:
During compilation, C ++ will combine function names and parameters to generate an intermediate function name to solve the problem of function polymorphism. C ++ does not, therefore, the corresponding function cannot be found during the link. In this case, the C function needs to use the extern "C" to specify the link. This tells the compiler to keep my name, do not generate an intermediate function name for the link for me.
The following is a standard syntax:
// On the. h File Header
# Ifdef _ cplusplus
# If _ cplusplus
Extern "C "{
# Endif
# Endif/* _ cplusplus */
...
...
// Where the. h file ends
# Ifdef _ cplusplus
# If _ cplusplus
}
# Endif
# Endif/* _ cplusplus */

5 questions:Extern function declaration
It is often seen that extern is placed before the function to become part of the function declaration. So, what is the role of the C language keyword extern in the function declaration?
Answer and analysis:
If the function declaration contains the keyword extern, it only implies that the function may be defined in other source files and has no other function. There is no obvious difference between the following two function declarations:
Extern int f (); and int f ();
Of course, such use is still available, that is, replacing include "*. h "to declare the function. In some complicated projects, I prefer to add the extern modifier before all the function declarations. The following example shows the reasons and advantages: "global variables modified with extern"

(1) The following statements are contained in test1.h:
# Ifndef TEST1H
# Define TEST1H
Extern char g_str []; // declare the global variable g_str
Void fun1 ();
# Endif
(2) In test1.cpp
# Include "test1.h"
Char g_str [] = "123456"; // defines the global variable g_str
Void fun1 () {cout <g_str <endl ;}
(3) The above is the test1 module, which can be compiled and connected. If we still have the test2 module and want to use g_str, we only need to reference it in the original file.
# Include "test1.h"

Void fun2 () {cout <g_str <endl ;}
The above test1 and test2 can be compiled and connected at the same time. If you are interested, you can use ultraEdit to open test1.obj. You can find the string "123456" in it, but you cannot find it in test2.obj. This is because g_str is the global variable of the entire project and only one copy exists in the memory. The test2.obj compilation unit does not need to have one more copy, otherwise, this error will be repeatedly reported during connection!
(4) Some people like to put together the Declaration and definition of global variables to avoid forgetting the definition. For example, change test1.h
Extern char g_str [] = "123456"; // This is equivalent to no extern
Then, remove the g_str definition in test1.cpp. When the test1 and test2 modules are compiled and connected again, a connection error is reported because you put the definition of the global variable g_str after the header file, test1.cpp this module contains test1.h so it defines g_str once, And test2.cpp also contains test1.h so it defines g_str again. At this time, the connector finds two g_str pairs when connecting test1 and test2. If you want to put the g_str definition in test1.h, replace # include "test1.h" in test2 code:
Extern char g_str [];
Void fun2 () {cout <g_str <endl ;}
At this time, the compiler will know that g_str is an external compilation module and will not be repeatedly defined in this module, but I would like to say this is very bad, because you cannot use # include "test1.h" in test2.cpp, other functions declared in test1.h cannot be used unless they are modified with extern, in this case, you need to declare a large string of functions, and the header file is used to provide external interfaces, so remember,It is always so simple to declare the truth in the header file..

6. extern and static

(1) extern indicates that the variable has been defined elsewhere. The variable must be used here.
(2) static is a static variable. When memory is allocated, it is stored in the static zone and not on the stack.

The scope of static is the relationship between internal connections, which is somewhat different from extern. it is stored separately from the object itself, and extern is also stored separately. However, extern can be referenced by other objects using extern, but static cannot. It only allows the object to use it. first of all, static and extern are a pair of "fire cannot be" guys, that is, extern and static cannot modify a variable at the same time. Second, the static modified global variables are declared and defined at the same time, that is to say, when you declare the global variable using static in the header file, it is also defined. Finally, the scope of static modification to the global variable can only be its own compilation unit, that is to say, its "global" is only valid for the current compilation unit, but not for other compilation units, such:
(1) test1.h:
# Ifndef TEST1H
# Define TEST1H
Static char g_str [] = "123456 ";
Void fun1 ();
# Endif

(2) test1.cpp:
# Include "test1.h"
Void fun1 () {cout <g_str <endl ;}
(3) test2.cpp
# Include "test1.h"
Void fun2 () {cout <g_str <endl ;}
The above two compilation units can be connected successfully. When you open test1.obj, you can find the strings "123456" in it, and you can also find them in test2.obj, the reason why they can be connected successfully without repeatedly defining errors is that although they have the same content, they store different physical addresses, just as two different variables assigned the same value, the two variables act on their respective compilation units. Maybe you are more serious. You secretly track and debug the above Code. As a result, you find that the memory address of g_str of two compilation units (test1, test2) is the same, so you can conclude that static modified variables can also be applied to other modules, but I want to tell you that your compiler is deceiving you, and most compilers have optimized functions for the code, in order to achieve the generation of the Target Program, the memory is reduced and the execution efficiency is higher. When the compiler connects to each compilation unit, it will copy only one copy of the memory with the same content, for example, in the above "123456", the variables in the two compilation units are the same content, so it will only exist in the memory during the connection, if you change the code above to the following, you can immediately crack the lies of the compiler:
(1) test1.cpp:
# Include "test1.h"
Void fun1 ()
{
G_str [0] = ''a '';
Cout <g_str <endl;
}

(2) test2.cpp
# Include "test1.h"
Void fun2 () {cout <g_str <endl ;}
(3) void main (){
Fun1 (); // a23456
Fun2 (); // 123456.
}
At this time, when you track the code, you will find that the g_str address in the two compilation units is not the same, because you modified it in one place, so the compiler is forced to restore the original memory, there are two copies in the memory to use the variables in the two modules. It is precisely because static has the above features that, when defining static global variables, it is usually put in the original file rather than the header file, so that it will not cause unnecessary information pollution to other modules, remember this principle!

7. extern and const

The global constants modified by const in C ++ have the same characteristics as static, that is, they can only act on this compilation module, however, const can be connected to extern to declare that this constant can be applied to other compilation modules, such as extern const char g_str [];
Do not forget to define const char g_str [] = "123456 ";

So when the const is used independently, it is the same as static, and when it is used together with extern, its features are the same as extern! So I can't describe const too much. I just want to remind you that const char * g_str = "123456" is different from const char g_str [] = "123465, the preceding const modifies char * rather than g_str. Its g_str is not a constant and is regarded as a defined global variable (which can be used by other compilation units ), therefore, if you want char * g_str to follow the global constant rules of const, it is better to define const char * const g_str = "123456 ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.