2022-03-25 by:CAE仿真在线 来源:互联网
A.5.2. External Variables
If you have a global variable that is declared in one source code file, but a function in another source file needs to use it, then it must be defined in the other source fi1e as an external variable. To do this, simp1y precede the variable declaration by the word extern as in
extern real volume;
If there are several files referring to that variable then it is convenient to include the extern definition in a header (.h) file, and include the header file in all of the .c files that want to use the external variable. Only one .c file should have the declaration of the variable without the extern keyword. Below is an example that demonstrates the use of a header file.
Important: extern can be used only in compiled UDFs.
Suppose that there is a global variable named volume that is declared in a C source file named file1.c
real volume; /* real variable named volume is declared globally */
/* code that computes volume of some zone */
volume = ....
If multiple sourc files want to use volume in the1r computations, then volume can be declared as an external variab1e in a header file (for example, extfile.h)
Header file that contains the external variable declaration for
extern real volume; /*声明udf全局变量,这样在h文件声明就可以了*/
Now another file named file2.c can declare volume as an external variable by simply including extfile.h.
#include "extfile.h" /* header file containing extern declaration
is included */
/* code that computes the per unit volume source using the total
volume computed in the compute_volume function from file1.c */
real tota1_source = ...;
source = total_source/volume;
The static operator has different effects depending on whether it is applied to local or global variables. When a local variable is declared as static the variable is prevented from being destroyed when a function returns from a call. In other words, the value of the variable is preserved. When a global variable is declared as static the variable is "file global." It can be used by any function within the source file in which it is declared, but is prevented from being used outside the file, even if is declared as external. Functions can also be declared as static. A static function is visible only to the source file in which it is defined.
Important: static variables and functions can be declared only in compiled UDF source files.
A.5.3.1. Example - Static Global Variable
/* mysource.c /*
static real abs_coeff = 1.0; /* static global variable */
/* used by both functions in this source file but is
not visible to the outside */
DEFINE_SOURCE(energy_source, c, t, dS, eqn)
real source; /* local variable
int P1 = ....; /* local variable
value is not preserved when function returns */
dS[eqn] = -16.* abs_coeff * SIGMA_SBC * pow(C_T(c,t),3.);
source =-abs_coeff *(4.* SIGMA_SBC * pow(C_T(c,t),4.) - C_UDSI(c,t,P1));
DEFINE_SOURCE(p1_source, c, t, dS, eqn)
int P1 = ...;
dS[eqn] = -abs_coeff;
source = abs_coeff *(4.* SIGMA_SBC * pow(C_T(c,t),4.) - C_UDSI(c,t,P1));