The problem with Extern is that we don’t know whether this keyword is a declaration or a definition when it appears.
Remember: declarations can be made multiple times, definitions can only be made once.
The extern keyword in function declaration is optional, because the function itself is extern if it is not modified. But it still needs to be declared when quoting.
When global variables are declared externally, the extern keyword is necessary. If the variable is not extern modified and not explicitly initialized, it also becomes the definition of the variable, so extern must be added at this time, and the compiler stores it in this tag The space is loaded into memory at execution time and initialized to 0. The declaration of local variables cannot be modified with extern, and local variables only allocate memory in the stack part at runtime.
Referential declarations, defining declarations
strong symbols, weak symbols
appear in the gcc link analysis of Linux, which can deepen the understanding of the link.
There is essentially no difference between global variables or functions. The function name is a pointer to the beginning of the function binary block. Global variables are variables declared outside the function. The function name is also outside the function, so the function is also global.
When using it, form a style.
Header file
First of all, let’s talk about the header file. In fact, the header file has no effect on the computer. It just expands it in the #include place during pre-compilation. It has no other meaning. In fact, the header file is mainly for others to see. .
I did an experiment, changed the suffix of the header file to xxx.txt, and then used
#include "xxx.txt"
to compile where the header file is referenced, and the link went smoothly. It can be seen that the header file is only for reading the code and has no other function!
Whether it’s C or C++, you put your functions, variables, structures, classes, etc. in your .c or .cpp file. Then compile it into lib, dll, obj, .o, etc., and then use the most basic gcc hisfile.cpp yourfile.o|obj|dll|lib and so on when others use it.
But for us programmers, how do they know what is in your lib, dll...? It depends on your header file. Your header file is a description to the user. Descriptions of functions, parameters, and various interfaces.
Since it is an explanation, what is placed in the header file is naturally the "declaration" about functions, variables, and classes. Remember, it's a "statement", not a "definition".
So, I assume you all know the difference between declaration and definition. Therefore, it is best not to define anything stupidly in the header file. For example, global variables:
#ifndef _XX_header file.H
#define _XX_header file.H
int A;
#endif
Then, what is very bad is that the int A here is the definition of a global variable, so If this header file is referenced multiple times, your A will be defined repeatedly
Obviously the syntax is wrong. But with this #ifndef conditional compilation, it can ensure that your header file is only referenced once, but it may still go wrong, but if multiple c files contain this header file, an error will still occur, because the macro name valid range It is limited to this C source file, so there will be no errors when compiling these multiple C files, but an error will be reported when linking, saying that you have defined the same variable in multiple places,
Linking...
incl2.obj : error LNK2005: "int glb" (?glb@@3HA) already defined in incl1.obj
Debug/incl.exe : fatal error LNK1169: one or more multiply defined symbols found
Attention! ! !
extern
This keyword is really hateful. When declaring, this extern can be omitted, so it will make you confused whether it is a declaration or a definition. The following is divided into two categories: variables and functions:
(1) Variables
Especially for variables.
extern int a;//Declare a global variable a
int a; //Define a global variable a
extern int a =0;//Define a global variable a and give it an initial value.
int a =0;//Define a global variable a and give it an initial value.
The fourth one is equal to the third one. They both define a global variable that can be used externally and give it an initial value.
You’re confused, they look really alike. But the definition can only appear in one place. In other words, whether it is int a; or extern int a=0; or int a=0;, it can only appear once, but that extern int a can appear many times.
When you want to reference a global variable, you have to declare extern int a; extern cannot be omitted at this time, because if it is omitted, it becomes int a; this is a definition, not a declaration.
(2) Function
Function, function, the same is true for functions, they are also defined and declared. When defining, use extern to indicate that this function can be referenced externally. When declaring, use extern to indicate that this is a statement. However, there is a difference between the definition and declaration of a function. A function definition must have a function body, while a function declaration does not have a function body. Therefore, extern can be omitted when defining and declaring a function. Anyway, other files also know that this function is defined elsewhere. , so it’s okay not to add extern. The two are so different that there is no problem if extern is omitted.
For example:
int fun(void)
{
return 0;
}
Very good, we defined a global function
int fun(void);
We made a declaration for it, and then we can It doesn’t matter whether you use
with or without extern
We can also put the declaration of fun in a header file, and it finally becomes like this
int fun(void);//Function declaration, so extern is omitted, which is more complete extern int fun(void);
int fun(void)
{
return 0;
}//A complete global function definition, because there is a function body, extern is also omitted.
Then, a client, a client who wants to use your fun, includes this header file, ok, a global statement. no problem.
However, correspondingly, if this customer wants to use global variables, then a certain variable must be extern; otherwise, it becomes a definition.
To summarize:
For variables, if you want to use a variable from another source file in this source file, you need to declare the variable with extern before use, or declare the variable with extern in the header file;
For functions, if you want to use a function from another source file in this source file, you need to declare the variable before use. It doesn’t matter whether you declare the function with extern or not, so you don’t need to add extern to the function in the header file. .
Declaration is used to explain the meaning of each identifier without pre-storing space for each identifier. The declaration of reserved storage space is called a definition. The form of declaration is: declaration specifier declarator declarator is composed of storage class specifier and type specifier.
1. There are two situations when declaring variables: One requires the creation of storage space.
For example: int a has already created a storage space when it is declared.
2. The other is that there is no need to create storage space.
For example: extern int a where variable a is defined in another file.
Example 1:
Declaration.
A construct which associates attributes to a variable name or function. No storage is reserved. For example:
extrn int a;
extrn char c;
variable declaration A structure decleration could look like:
Definition.
Variable definition is a declaration with storage allocation.
struct per_rec
{
int age;
char *surname;
char *firstname;
};
int a;
char c;
struct per_rec person;
A construct which specifies the name, parameters and return type of a function. For example a function definition would be:
long sqr(int num)
{
return(num*num );
}
The former is a "defining declaration" or "definition", while the latter is a "referencing declaration". From a broad perspective, declarations contain definitions, but not all declarations are definitions. For example: int a is both a declaration and a definition. However, for extern a, it is only a declaration and not a definition. It can be declared repeatedly in the same source program or in different source programs. Under normal circumstances, we often describe it this way, calling the statement that creates a space a "definition", and calling a statement that does not require the creation of a storage space a "declaration". Obviously, the statement we are referring to here is relatively narrow in scope, that is to say, a non-definition statement.
For example: in the main function
int main()
{
int a; //Here is the definition (declaration of allocating space), it cannot be repeated
//If you write extern int a; or int here a; When compiling in VC6.0, an error is reported for duplicate definition
//(redefinition)
//Write int a here; when compiling in DEV-C++, an error will be reported (redeclaration)
//Write extern int a here; there will be no problem compiling and running in DEV-C++
extern int A; //This is a declaration rather than a definition. The declaration A is an already defined external variable
//Note: You can remove the variable type when declaring an external variable, such as: extern A;
dosth(); //Execute the function
}
int A; //It is a definition, which defines A as an external variable of integer type
It seems that definition or declaration sometimes has something to do with the processing of the compiler.
The "definition" of an external variable is different from the "declaration" of an external variable. The definition of an external variable can only be once, and its location is outside all functions, while the declaration of external variables in the same file can be Multiple times, it can be inside a function (whichever function is to be used is declared in that function) or outside the function (before the definition point of the external variable). The system allocates storage space based on the definition of external variables (rather than the declaration of external variables). For external variables, initialization can only be done in the "definition", not in the "declaration".
The so-called "declaration" is to declare that the variable is an external variable that has been defined later. It is only a "declaration" made to reference the variable "in advance". extern only makes a statement and does not make any definitions.
Using static to declare a variable has two functions:
(1) For local variables to be declared static, the space allocated for the variable will always exist throughout the execution period of the program.
(2) If an external variable is declared static, the variable's role is limited to this file module.
#include "stdafx.h"
1.extern is often used in variable declarations. You declare a global variable in the *.c file. If this global variable is to be referenced, Place it in *.h and declare it with extern.
2. If the keyword extern is included in the function declaration, it only implies that the function may be defined in other source files and has no other effect. That is, there is no difference between the following two function declarations:
extern int f(); and int f();
==================== ===========
If the c/cpp file that defines the function declares the defined function in the corresponding header file, then to use these functions in other c/cpp files, you only need to include This header file is enough.
If you don't want to include the header file, then declare the function in c/cpp. Generally speaking, "extern" is not used to declare functions defined in this file, and "extern" is used to declare functions defined in other files. In this way, when calling functions defined in other files in this file, there is no need to include header files
include "*.h" to declare the function, you can use it directly after declaration.
================================
For example:
//extern. The content of cpp is as follows:
// extern.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
extern print(char *p);
int main(int argc, char* argv[])
{
char *p="hello world!";
print(p);
return 0;
}
//The content of print.cpp is as follows
#include "stdafx.h"
#include "stdio.h"
print(char *s)
{
printf("The string is %sn",s);
}
The result is that the program can run normally and output the results. If you remove "extern", the program can still run normally.
It can be seen that "extern" is dispensable in the function declaration. It is only used to indicate whether the function is defined in this file or in another file. As long as your function is declared before use, you don't need to include the header file.
In C language, the modifier extern is used before the declaration of a variable or function to indicate "this variable/function is defined elsewhere and should be referenced here."
0. Extern modifies the declaration of variables. For example, if file a.c needs to reference the variable int v in b.c, you can declare extern int v in a.c, and then you can reference the variable v. What needs to be noted here is that the link attribute of the referenced variable v must be an external link (external). That is to say, for a.c to reference v, it not only depends on the declaration of extern int v in a.c, but also depends on the variable v itself. can be quoted. This involves another topic in the C language-the scope of variables. Variables that can be referenced by other modules with the extern modifier are usually global variables. Another very important point is that extern int v can be placed anywhere in a.c. For example, you can declare extern int v at the beginning of the definition of function fun in a.c, and then you can refer to the variable v. That's all. You can only reference v in the scope of function fun. This is still a problem of variable scope. Regarding this point, many people have concerns when using it. It seems that the extern statement can only be used in file scope.
1. extern modified function declaration. Essentially, there is no difference between variables and functions. The function name is a pointer to the beginning of the function's binary block. If file a.c needs to reference a function in b.c, for example, the prototype in b.c is int fun (int mu), then you can declare extern int fun (int mu) in a.c, and then you can use fun to do anything. Just like the declaration of a variable, extern int fun (int mu) can be placed anywhere in a.c, and does not necessarily have to be placed in the file scope of a.c. The most common way to reference functions in other modules is through header files containing declarations of those functions. What is the difference between using extern and including a header file to reference a function? The extern reference method is much simpler than including the header file! The use of extern is straightforward. Use extern to declare which function you want to reference. This is probably a manifestation of the KISS principle! An obvious benefit of doing this is that it will speed up the process of program compilation (preprocessing, to be precise) and save time. This difference is very obvious during the compilation of large C programs.
2. In addition, the extern modifier can be used to indicate the calling specification of C or C++ functions. For example, to call a C library function in C++, you need to use extern "C" to declare the function to be referenced in the C++ program. This is used by the linker to tell the linker to use the C function specification when linking. The main reason is that the naming rules in the target code after C++ and C programs are compiled are different
For more extern usage summary related articles, please pay attention to the PHP Chinese website!