In large projects, we will encounter many source files.
File a. c Static int I; // only used in file Int J; // used in the project Static void Init () // only used in file { } Void callme () // used in the project { Static int sum; }
|
The above global I variables and init () functions can only be used in the. c file, and the scope of the global variable sum is only in callme. The limitations of variable J and function callme () are extended to the entire project file. Therefore, you can use the extern keyword to call in B. C below. Extern tells the compiler that the variable or function has been defined in other files.
File B .C Extern Int J; // call Extern void callme (); // call Int main () { ... }
|
In addition, when C and C ++ are mixed programming, if C ++ calls a function or variable defined in the C source file, so we need to add extern to tell the compiler to name the function in C mode:
File a. cpp calls the variables I and callme () in A. C () Extern "C" // call the variable in the C file in the C ++ File { Int J; Void callme (); } Int main () { Callme (); }
|