I. extern and functions
Once mentioned: if a program has multiple source files (. c), multiple target files (. OBJ), these target files cannot be run independently, because these target files may be associated, such as. OBJ may call C. A function defined in OBJ. Only when these Associated target files are linked together can an executable file be generated.
Let's first understand two concepts:
- External functions: functions defined in the current file can be accessed and called by other files. According to the C language, external functions with the same name are not allowed.
- Internal function: If the function defined in the current file cannot be accessed or called by other files, it can only be used internally. It is called an internal function. The C language specifies that different source files can have internal functions with the same name and do not interfere with each other.
Next we will demonstrate how to call the function defined by another source file in a source file, for example, calling the one function defined in one. c In Main. C.
1. First define a one function in one. C.
If you want this one function to be accessible by main. C, one function must be an external function. The complete definition is to add the extern keyword.
However, this extern is the same as the auto keyword and can be omitted, because by default, all functions are external functions. We can simplify the process as follows:
2. Next, in the main function of Main. C, call the one function in one. C.
How can we call one function in one. C? You may have two ideas:
Idea 1: write one () directly in the main function ();
This is definitely not a good practice, because the main function does not know the existence of one function. How can we call it? This error will be reported in the Standard C compiler, but it is only a warning in xcode.
Idea 2: Include the one. c file in Main. c
As we all know, # the function of include is to copy content, so it is equivalent
Well, it seems like this is true. I defined a one function before the main function and called this one function in the main function. The syntax is correct, so compilation is fine. However, this program cannot run successfully because an error is reported during the link. We are already at one. c defines the one function, and now in main. the one function is defined in C. The C language does not allow external functions with the same name. When linking, the linker will find the one function. OBJ and Main. if the same function is defined in OBJ, an error is reported directly. The error message in xcode is as follows:
Duplicate symbol _ One indicates that the one identifier is repeated, and linker indicates the linker.
The above two ideas are not feasible. In fact, the idea is the same: Let the main function know the existence of one function. The correct method should be to declare one function in advance before the main function (clearly, it is declaration, not definition, and definition and declaration are two different things ).
3. Declare one function in advance before the main function
If you want to claim the external functions defined in other source files, you should use the extern keyword to reference others' "external functions"
Run the program and output it from the console. You can find that the one function defined in one. C has been successfully called by the main function of Main. C.
Some people may immediately come up with an idea: What should we do if there are other source files that define this one function in addition to one. C? Who exactly calls the main function? Don't worry, this is definitely not the case. Just now I didn't mean that we can't define the same external function repeatedly. Otherwise, the linker will report an error, so there will be only one external one function.
The above is the effect of the extern keyword on the function: used to define and declare an external function. In fact, extern is useless like auto, and can be omitted. Therefore, we can simplify it as follows:
For modular development, in a formal project, we will write the one function declaration to another header file. Of course, the naming of this header file should be meaningful and standardized, for example, one. h. In the future, anyone who wants to call this one function will be able to include the one. h header file. The final code structure is as follows:
Ii. Static and functions 1. Define internal functions
The preceding example shows that the one function defined in one. C can be accessed by other source files. In fact, sometimes we may want to define an "internal function", that is, we do not want other files to access the function defined in this file. This is very simple. You only need to add a static keyword when defining the function.
(We will modify the code above)
I added a static parameter in front of void one () to indicate that one function is an internal function.
Then you will find that the program cannot run and an error is reported during the connection. The reason for the error is simple: in main. c calls one. the one function defined in C, but now one. c's one function is an "internal function" and cannot be accessed by other files. Let's take a look at the error message:
The undefined symbols in the 1st red boxes... indicates that the one identifier is not defined, that is, one cannot be found. The linker in the 2nd red boxes indicates that the linker reports an error.
However, this program can be compiled successfully, because we declare the one function before the main function (the Declaration and definition of the function are two things). This function declaration can be understood as: In terms of syntax, lie to the main function and tell it that one function exists. Therefore, the main function can call one function in terms of syntax. Does this one function exist? Is it defined? The compiler does not care. In the compilation phase, the compiler only checks whether the syntax of a single source file is unreasonable and does not check whether the function is defined. It only checks whether the function is stored or not when linking.
Let's discuss another question. Why can compilation be successful in many cases, but an error is reported during the link? As long as you understand the functions of compilation and linking, it is easy to do.
The so-called compilation is to separately check whether the syntax of each source file is reasonable, and do not check the association between each source file. A source file is generated after it is compiled successfully.
The so-called link is to check the association relationship of the target file, and combine the associated target files to generate executable files.
After reading these two concepts, I should be able to fully understand the previous errors.
2. Declare internal functions
We can also use static to declare an internal function.
1 # include <stdio. h> 2 3 static void test (); 4 5 Int main (INT argc, const char * argv []) 6 {7 test (); 8 return 0; 9} 10 11 static void test () {12 printf ("test function called"); 13}
Define a test function in row 11th. This is an internal function. Then, declare the test function in row 3rd in advance, and then you can call the test () function in row 7th.
Iii. Summary of static, extern and functions 1. Static
* When defining a function, you can declare the function as an internal function (also called a static function) by adding static on the far left of the function ), in this way, the function can only be used in the file where its definition is located. If there are internal functions with the same name in different files, they do not interfere with each other.
* Static can also be used to declare an internal function.
2. extern
* When defining a function, if the keyword extern is added to the leftmost of the function, this function is an external function and can be called by other files. The C language specifies that if extern is omitted when defining a function, it is implicitly an external function.
* To call external functions in other files in a file, you need to declare the external function with extern in the current file, and then you can use it. The extern here can also be omitted.
Study Notes 19-static and extern keywords 1-Functions