C language Extern declaration analysis

xiaoxiao2021-03-06  99

C language

EXTERN declaration analysis

1 Basic Explanation Extern can be placed in a variable or function to mark the variable or function in other files, prompting the compiler to seek its definition in other modules when the compiler encounters this variable and function. In addition, Extern can also be used for link designation. 2 Question: The extern variable defines an array in a source file:

Char a [6]; in another file, use the following statement to declare:

Extern char * a; Can you ask, can you? Answers and analysis: 1), not, the program will tell you illegal access. The reason is that the pointer to the type T does not equal the array of type T. EXTERN CHAR * A declares is a pointer variable rather than a character array, so it is different from the actual definition to cause illegal access to runtime. The statement should be changed to Extern Char A []. 2), example analysis, if a [] = "abcd", the external variable A = 0x61626364 (ABCD ASCII code value), * a obviously meaningless, as shown below: Obviously a point to space (0x61626364) meaningless, Easy to have illegal memory access. 3), this prompts us, in the format of the declaration, in actual programming, such mistakes are frequent in actual programming. 4) EXTERN often has such a role in the variable declaration, you declare a global variable in the * .c file, if you want to be referenced, put it in * .h and use Extern to declare. 3 Question: Extern Functions 1 often see extern placed in front of the function as part of the function declaration, then what role in the C language keyword EXTERN does the function of the function? Answer and Analysis: If the function's declaration is with keyword extern, it is only implicit that this function may be defined in other source files, without other functions. That is, the following two functions have no obvious difference:

Extern int f (); and int F (); of course, this is still in the program, in the program, replacing the include "* .h" to declare the function, in some complex projects, I am used to all functions Add externaln modifications before declaring. 4 Question: EXTERN Function 2 When the function provider is unilaterally modified, if the usage does not know the original Extern declaration, the compiler will not report an error. However, during operation, because less or more input parameters, it is often necessary to make a system error. How should this situation solved? Answers and analysis: The current industry does not have a perfect solution for this situation. The usual approach is to provide a statement on external interfaces in our xxx_pub.h, and then call the partial file file, so This step of Extern. To avoid such errors. The sword has a double front, the application of Extern, and the different occasions should choose different practices. 5 Question: EXTERN "C" When using the C function in the C environment, the compiler often occurs if the compiler cannot find the C function definition in the OBJ module, which leads to the failure of the link, how should I solve this situation? Answers and analysis: C language In order to solve the polymorphic issues of the function, the C language will join the function name and parameters to generate a middle function name, and the C language will not, and therefore can't find the corresponding function when the link is caused. Situation, at this time, the C function needs to be used with Extern "C", which tells the compiler, keep my name, do not generate the intermediate function name for the link. Here is a standard way of writing: // On the head of .h file #ifdef __cplusplus # {__cplusplusextern "c" {#ndif #Endif / * __cplusplus * / ... //.h file ended #ifdef __cplusplus #if __cplusplus} # endif # endif / * __cplusplus * /

转载请注明原文地址:https://www.9cbs.com/read-95512.html

New Post(0)