C language is organized as a file file, and in all files of a source program, an external variable and function can only be defined once in the source program. The static and extern keywords appear because different functions and variables in different files often refer to each other. The static function is mainly introduced here, which is a summary of today's compilation problem.
Have learned C + + should know, class member has three kinds of types: public,protected,private. Where the private keyword indicates that the field can only be accessed by the member function of the class and is not visible outside the class. The static keyword in C is similar to this: whenever a function with the static keyword is added, it is visible only to the file that contains the static function, and is invisible outside the file. Let me give you an example to illustrate
Here is the main function, which calls the Hello function to print the word group buf, located in the file test.c.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main ()
{
Char buf[] = "Hello world!\n";
Hello (buf);
return 0;
}
The Hello function is in hello.c
#include <stdio.h>
#include <stdlib.h>
static void Hello (char *buf)
{
Write 1,buf, sizeof buf);
}
Note that the Hello function contains the static keyword
After compiling
GCC hello.c test.c-o Test
The following error will appear
/tmp/ccu7gziz.o:in function ' main ':
test.c: (. text+0x3f): Undefined reference to ' hello '
collect2:error:ld Returned 1 exit status
Indicates that the Hello function cannot be found, and then we try to change the Hello function to a non static type. Repeat the above process to successfully generate the target code to run.
As you can see, the function containing the static keyword is visible only in the file in which it is located and is not visible in other files, resulting in no definition being found.