Document directory
- What is an internal connection external connection?
- Relations between CPP source file and H header file
- Extern external Declaration
- Static internal connection
- Const keyword
- Inline and static functions are also internally connected.
What is an internal connection external connection?
We know that when compiling (if the compiler is vs), the object is compiled into OBJ files one by one in units of the CPP file of the source file, and then linked different OBJ files through the linker.
To put it simply, if some variables or functions are defined as inner connections, the linker will not compare them with OBJ to see if there are repeated definitions, the variables or functions declared by the extern in one source file cannot use the variables or functions connected in the other source file. if it is an external connection, you need to compare whether there is a redefinition in different obj. in addition to this check, the linker will also view the definitions of variables or function declarations modified through extern in other obj.
Relations between CPP source file and H header file
We know that C ++ supports a variety of programming paradigms and can use process-oriented methods rather than classes. Of course, few people do this. Generally, they combine process-oriented and object-oriented methods.
If there are some variables and functions in different source files (not variable members or member functions in the class), how should we use variables or functions in different source files? When we use classes in object-oriented systems, it is okay to reference them with header files. But there are no classes, just variables and functions. Can we also reference header files? This depends on the situation. if only external declarations are not defined in the header file, it is no problem to reference the header file. if the header file has a definition, it is okay if it is referenced by only one CPP file. If it is referenced by multiple CPP files, a duplicate definition error will occur. (Note: The compiler is compiled in units of CPP files. If an H header file is not referenced, it is equivalent to being discarded. when a header file is referenced, the pre-compilation simply copies the header file to the CPP file that references it .)
Extern external Declaration
Suppose there are two source files: One. cpp and two. cpp.
// In one. cpp
---------------------------------------------------
# Include <iostream>
Using namespace STD;
Int number = 123; // number definition. Or, it can be written as extern number = 123. When there is a value assignment, extern actually loses its role. Therefore, adding or not does not affect the operation.
Void print () {cout <"Hi, I am one. cpp" <Endl ;}
// In two. cpp
--------------------------------------------------------
# Include <iostream>
Using namespace STD;
Extern int number; // This is the so-called external declaration. extern cannot be saved here. In addition, it cannot be assigned here. If it is written as extern int number = 88, an error is returned, which is a duplicate definition.
Extern void print (); // extern can be omitted here.
Cout <number; // Result 123
Print (); // output I am one. cpp
In two. in CPP, how does one get one. in CPP, the value of number is declared as extern int number, which indicates that number is defined in other source files, and the linker will help you find the number in other source files.
Assume that the above two. remove the extern keyword in CPP. no errors during compilation. however, an error occurred while linking and the link was already defined. because one. A number has been defined in CPP, and the same number cannot be defined.
Static internal connection
In the above example, we know that writing int number in one. cpp and two. cpp at the same time will lead to an error, indicating that the definition is repeated.
// In one. cpp
Static int number = 123;
// In two. cpp
Static int number; // if no value is explicitly assigned, 0 is assigned by default.
At this time, no error occurs. this is because the variables are defined by external connections by default. when the keyword "static" is added, it indicates a static variable and an internal connection. The linker does not check the static variables with duplicate names in the OBJ files compiled by different CPP.
After static modification, you cannot use extern.
// In one. cpp
Static int number = 123;
// In two. cpp
Extern int number;
Cout <number;
In this case, an error occurs because the number declared by extern cannot be defined, because the number of one. cpp is modified using static to indicate that it is an internal connection.
Const keyword
// In one. cpp
Const int number = 123;
// In two. cpp
Const int number = 321;
The effect achieved here is the same as that of static, and both belong to internal connections, so no error will be made. the only difference is that the const represents a constant, and the value must be explicitly assigned during definition, and the value cannot be changed after the value is assigned.
However, another feature of const is that it can be used with extern.
For example, in two. cpp
Extern const int number;
Cout <number; // an error will be reported during the running, indicating that the definition cannot be found. You need to change one. cpp to extern const int number = 123.
// The correct output value is 123 in one. cpp.
Inline and static functions are also internally connected.
// In one. cpp
Void test (){}
// In two. cpp
Void test (){}
In this way, an error is reported during compilation, and the definition is repeated. however, if you change both the preceding void test to inline void test () {} or static void test () {}, no error will occur. // note that the inline function here refers to the global function, not the inline function in the class.
Therefore, functions are similar to common variables. if no modification is made, external connections are used by default, and internal connections are used for static modification. in addition, there is no const function. You can add a const to the function only in the class.