Not compilation at first ~
Later I saw Grandpa Tan's book.
When you encounter extern during compilation, first find the definition of the external variable in this file. If it is found, it will extend the scope in this file. If it cannot be found, find the definition of external variables from other files during the connection. If the definition is found, the scope will be extended to this file. If the definition cannot be found, handle the error.
Different files are not directly compiled when they are connected.
[Ktktkt @ Jintao common] $ cat file1.c
# Include <stdio. h>
Extern int multiply (int A, int B)
{
Int C;
C = A * B;
Return C;
}
[Ktktkt @ Jintao common] $ cat file2.c
# Include <stdio. h>
Extern int sum (int A, int B)
{
Int C;
C = A + B;
Return C;
}
[Ktktkt @ Jintao common] $ cat extern. c
# Include <stdio. h>
Int main ()
{
Extern int multiply ();
Extern int sum ();
Int A, B;
Int result;
Printf ("Please input a and B/N ");
Scanf ("% d", & A, & B );
Result = multiply (A, B );
Printf ("the result of multiply is % d/N", result );
Result = sum (A, B );
Printf ("/nthe result of sum is % d/N", result );
Return 0;
}
[Ktktkt @ Jintao common] $ GCC file1.c file2.c extern. C-o extern
[Ktktkt @ Jintao common] $./extern
Please input A and B