extern declaration header file defines &

 https://www.cnblogs.com/tshua/p/5741009.html

With #include the header file can contain other variables, function declarations, why extern keyword?

       If I want to reference a global variable or a function, I just included directly in the source file #include <xxx.h> (xxx.h contains a declaration) Why can not it, why use extern it? ?

       This problem has been bothering me is paradoxical for a long time, through practice and find information, the following summary:

First, the header file

        First, let me nod document, in fact, no effect in terms of the header file for the computer, she just launched in the pre-compile time where #include the moment, no other meaning, in fact, is the main header file for others to see.

        I did an experiment, the suffix header files into xxx.txt, then reference the header files in place by

   #include"xxx.txt"

        Compilation, linking very well passed, can be seen, only the header files to read the code role, a role no other!

        Either the C or C ++, you put your function, variable, or structure, like what's in your .c or .cpp file. Then compiled into lib, dll, obj, .o and so on, and then when people use the most basic gcc hisfile.cpp yourfile.o | obj | dll | lib and so on.
        But for our programmers, they know how your lib, dll ... which in the end what? Depends on your header file. Your first document is to explain to the user. Function parameters, a variety of interface description.
        Since it is a description that, then put inside the header file is about natural functions, variables, class "declaration" (a function, the function prototype is also called) a. Remember, the "Statement", not "define."

      The difference between declarations and definitions.

Copy the code
    Declare variables, there are two cases: 

    1, one is the need to create storage space. For example: int a statement at the time had already established storage space. 

    2, the other is the need to establish storage space. For example: extern int a wherein a variable is defined in another file. 

    Statement is to introduce to the compiler name - an identifier. It tells the compiler "This function or variable can be found somewhere, what it looks like." 

    The definition says: "Here the establishment of the variable" or "function established here." It allocates storage for the name. Whether defined as a function or variable, the compiler must allocate storage space for them in the definition of point. For variables, the compiler determines the size of the variable, and then open up space in memory to store its data for the function, the compiler generates code that ultimately have to take up some memory. 

    Anyway: the declaration of the establishment of space become a "definition", the storage space is not required to establish a "statement." 

    Declare and define the basic types of variables (initialization) are produced simultaneously; for objects, declarations and definitions are separated. 

    For example: Class A 

    if A a; is a statement that tells the compiler to a variable is an object of class A, but are not initialized; 

    if after a = new A (); this is the initialization, allocated space. 

    (Our ultimate aim is to advance the use of the statement, that the definition used before, if you do not use a separate statement in advance is not necessary, and is so variable, function, too, so the statement does not allocate storage space, only the definition will . allocate storage space) 

    used to declare a variable static role is twofold: 

    (1) with respect to static local variable declaration, space is allocated for the variable is always present during the execution of the entire program.

    (2) an external variable declared with the static, then the effect of the variable module is limited to the present document. 


    Added: 

    What is the definition? What is a statement? What is the difference between them? 

    The so-called defined is (compiler) to create an object, allocates a block of memory for the object, and give it a name, which is what we often call the variable name or object name. 

    There are 2 re-meaning statement: 

    (1) tells the compiler that the name has to match the piece of memory, the following code uses variables or objects are defined elsewhere. Statement can appear multiple times. 

    (2) tells the compiler that the name has been scheduled, and elsewhere can no longer use it as a variable name or object name. 

    The most important difference between definitions and declarations is to: 

    define and create objects to this object allocated memory, the statement did not allocate memory.
Copy the code

 

 

So, it is best not stupid hee hee definition of what is in the header file. For example, a global variable:
  / * xx header file * /
  #ifndef _XX_ header files .H
  #define _XX_ header files .H
  int A;
  #endif

        then it is worse, here is the definition of int A global variable, so If the header file is referenced multiple times, then your definition of a will be repeated, apparently on the wrong syntax. With this just #ifndef conditional compilation, so can guarantee your header file is referenced only once, but probably still will not go wrong, but if multiple c file contains the header files will still wrong, because the macro name is valid only to the extent of this c source files, so that multiple files c compiler is not wrong, but it will error at link time, many say you define the same variable,

  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

  note! ! !

Two, extern

        This keyword is really more hateful in the definition variable, the extern actually may be omitted (the definition of default are omitted); when you declare a variable, this must be added before the extern variable, so sometimes you do not make clear in the end is declared or defined. Or that there is not necessarily a statement before the extern variable, and no extern variables can only be defined before. Note: To define the allocated memory space for the variable; and the statement does not need to allocate memory space for the variable.

     The following sub-categories for variables and functions:

(1) variable

  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 and a given initial value.
  int a = 0; // define a global variable a, and to the initial value,

       the fourth is equal to a third, a global variables are defined that can be used outside and to the initial value.
       Confused it, they really look like. But the definition can only appear in one place. In other words, whether int a; or extern int a = 0; or int a = 0; can only occur once, and that can extern int a lot of times.

       When you want to reference a global variable, you would have to declare, extern int a; extern at this time can not be omitted, since omitted, it becomes int a; this is a definition, not a statement. NOTE: extern int a; int may be omitted in type, i.e. extern a; but other types can not be omitted.

 

(2) function is
       a function for function, too, is the definitions and declarations, the definition of when to use extern, this function is explained that can be referenced outside, when it was declared with extern stated that this is a statement. However, due to definitions and declarations of functions are different, defined functions have the function body, declare the function does not function body (also end with a semicolon), it can be omitted when the extern function definitions and declarations, anyway, other documents are also We know that this function is defined elsewhere, so I do not add extern is required. Both so different, so there is no problem extern omitted.
   For example:
  / * a cpp files * /
  int Fun (void)
  {
        return 0;
  }

  well, we define a global function
  / * another cpp files * /
  int Fun (void);
  we made a statement on it, then later you can use the
  plus without extern all the same
  , we can also put a statement on the fun of in a header file, eventually became such
  /*fun.h*/
  int fun (void); // function declaration, so extern omitted, some are full int Fun extern (void);
  / * file corresponding fun.cpp * /
  int Fun (void)
  {
       return 0;
  } // define a complete global function, because the function body, the same extern It has been omitted.
       Then, a client, a fun to use your client, this header file, included into, ok, a global statement. no problem.
However, correspondence, if it is the customer wants to use global variables, to a certain extern variable; otherwise it becomes defined.

        to sum up:

        Variable, if you want to use another source (such as a file name B) in the source files (such as file names A) the variables, there are two methods: (1) must be declared extern in file B in A variables defined in this file (of course, global variables); (2) add file B corresponding header file a file, of course, the header includes variable declarations B file, i.e. must extern declared in the header file the variable, otherwise the variable has been defined once.

       For the function, if you want to use another source file (e.g., file name B) in the source file (e.g., file name A) is a function, there are two kinds of methods: (1) extern statement A file with the file B defined function (in fact, may be omitted extern, B file defines only function prototypes can occur in file a); (2) B is added in the header file corresponding to the file a, of course, contains the header file B function prototype in the header file function can not add extern.

******************************************************************************************************************************************************

       The above summary put it another way:

         (A) Global variables call for a file to another file, because the global variable is usually defined in the original document .c, we can not contain the source file header may only contain #include files, so commonly used method is to use extern int a to declare external variables. Another approach is to define a global variable int global_num in ac file, you can write extern in the corresponding header file int global_num ah, so that other source files can be declared by external variables include ah she is on it.

      (B) there are different functions and variables Examples

          int fun (); and extern int fun (); all statements (definitions have achieved the body). With extern int fun () but more explicitly indicated to declare it.

         And int a; is defined      

               extern int a; a statement.

 

(3) Further, extern modifier can be used in a C ++ program Standardization c function call.

 For example, calling the C library functions in C ++, you need to function extern "C" statement to be referenced in the program using C ++. This is the link Used to tell the linker at link time with the C function specification link. The main reason is the C ++ and C programs compiled naming different in the object code.

C ++ language at compile time to solve the problem of multi-state, and the parameter name will combine to generate a middle name, and c language does not, it will cause the situation can not find the corresponding link, then you need to use C extern "C" designated link, which tells the compiler, please keep my name, do not give me generation middle name for the link.

 

Third, the header files and link extern
        this link also solves two problems initially proposed:

  (A) may include other header files variable, the function declaration with #include, why extern keyword?

       (B) if I want to refer to a global variable or a function, I just included directly in the source file #include <xxx.h> (xxx.h contains a declaration) Why can not it, why use extern it? ?

         Answer: If a file (assuming the file name A) for a large number of files referenced variable or function to another (assuming the file name B) defined in the header file is used more efficiently, and more standardized program structure. Other files (such as file names C, D, etc.) for reference of variables defined in the file name B, simply #include the files B corresponding header file (of course, only the header of a variable or function declaration, there must not be defined) can be.

********************************************************************************************************************************************

       It was a forgotten era, when the compiler only know .c (or .cpp) file, .h not know is what's the matter.
       When people write a lot of .c (or .cpp) file, gradually, it was discovered that declaring a variable or function prototypes in many .c (or .cpp) file is the same, but they had a word a word to be repeated for each content knock .c (or .cpp) file. But even more frightening is that, when a statement in which there is a change, you need to check all .c (or .cpp) file, and modify the declaration which, ah ~, is simply coming end of the world!
       Finally, someone (perhaps Some people) can no longer endure this torture, he (they) will be repeated portion is extracted, placed in a new file, and then typing this sentence #include XXXX in need of .c (or .cpp) file. So even if a change occurs statement, also no longer need to find and modify --- around the world still so beautiful!
       Because this new file, often on the head .c (or .cpp) file, so give it named called "header files", extension is .h.
       Since then, the compiler (which is actually a pre-processor) to know the world in addition to .c (or .cpp) file, there is a .h file, and called #include command.

Guess you like

Origin www.cnblogs.com/wllwqdeai/p/10972209.html