Some libaries or code blocks have variables which they export. You can access these variables much in the same way as external functions. To access an external variable, you declare it as follows:
Var MyVar : MyType; external name 'varname';The effect of this declaration is twofold:
A second possibility is the declaration:
Var varname : MyType; cvar; external;The effect of this declaration is twofold as in the previous case:
In order to be able to compile such statements, the compiler switch -Sv must be used.
As an example, let's look at the following C file (in extvar.c):
/* Declare a variable, allocate storage */ int extvar = 12;And the following program (in extdemo.pp):
Program ExtDemo; {$L extvar.o} Var { Case sensitive declaration !! } extvar : longint; cvar;external; I : longint; external name 'extvar'; begin { Extvar can be used case insensitive !! } Writeln ('Variable ''extvar'' has value : ',ExtVar); Writeln ('Variable ''I'' has value : ',i); end.Compiling the C file, and the pascal program:
gcc -c -o extvar.o extvar.c ppc386 -Sv extdemoWill produce a program extdemo which will print
Variable 'extvar' has value : 12 Variable 'I' has value : 12on your screen.