In Delphi, there are also precompiled instructions similar to C. Although this type of instruction is only effective in the current single file (it may also be that the author does not fully understand the actual usage of this type of instruction), this type of instruction is useful for multi-version compilation. Production work (such as publishing a learning version from the standard version) does have quite good uses.
one. Instructions introduction:
1. DEFINE directive:
Format: {$DEFINE name}
Description: Used to define a symbol (Symbol) that is valid in the current unit. defined
You can then use the IF DEF and IFNDEF instructions to determine whether the symbol exists.
2. UNDEF command:
Format: {$UNDEF name}
Description: Used to cancel a symbol (Symbol) that has been defined in the current unit. This directive and DEFINE
used together.
3. IFDEF directive:
Format: {$IFDEF name}
Note: If the name after this directive has been defined, the code segment after this directive until {$ELSE} or {$ENDIF} will be compiled.
4. IFNDEF directive:
Format: {$IFNDEF name}
Note: If the name after this directive is not defined, the code segment after this directive until {$ELSE} or {$ENDIF} will be compiled.
5. IFOPT instruction:
Format: {$IFOPT switch}
Note: If the switch after this instruction has been set, the code segment after this instruction until {$ELSE} or {$ENDIF} will be compiled.
Example: {$IFOPT R+}
Writeln('Turn on the range checking switch when compiling');
{$ENDIF}
6. ELSE instruction:
Format: {$ELSE}
Description: Determine whether the code segment between the instruction and {$ENDIF} should be compiled or ignored by judging the conditional expression of the prefix Ifxxx.
7. ENDIF directive:
Format: {$ENDIF}
Description: Cooperates with Ifxxx to indicate the end position of the source code segment of the conditional precompiled segment.
two. example:
Write examples to perform compilation work without code segments by pre-defining different compilation symbols.
1. Create a new Delphi project and add a Button button on the form of Unit1 unit.
2. Write the program as follows:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
PRocedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{Private declarations}
public
{Public declarations}
a : String;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{$DEFINE AAA} // Definition line.
procedure TForm1.FormCreate(Sender: TObject);
begin
a := 'Other';
{$IFDEF AAA}
a := 'AAA';
{$ENDIF}
{$IFDEF BBB}
a := 'BBB';
{$ENDIF}
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := a;
end;
end.
{Note: The bold text is the entered code}
3. After compiling and running, press the Button, and you will see "AAA" displayed in the title bar of the form. The program compiles the statement a := 'AAA'.
4. Change the program segment that defines the line:
when changed to
{$DEFINE BBB}
When you compile and run again, you will see "BBB" displayed in the title bar of the form. The program compiles the statement a := 'BBB'.
When undefining a row or changing to
{$DEFINE NOTHING}
or other names, compile and run again, and you will see "Other" displayed in the title bar of the form. The program only compiles the statement a := 'Other'.
three. How to quickly create and change versions:
Using precompilation instructions, when making multiple versions of the same program, you only need to find the different units in each version, define unified version symbols (Symbols) in sequence, and then add conditional precompilation instructions to the program segment. You can choose to compile different program parts during actual compilation, which has a good effect on the standardization of the program (defining a unified version symbol) and confidentiality (different versions compile different program parts).
However, since this type of precompiled directive can only act on the current unit, the inconvenience is that the version symbol cannot be defined once in a common unit, but a unified version symbol must be defined in each unit. Therefore, when changing versions, You need to make sure that all version symbols have been changed to ensure the correctness of each version. For this, you can use the "Find in" function of Delphi IDE. Files..." (search strings in multiple files) function to find all files and locations that define version symbols, and then change them in sequence to ensure that all locations have been corrected.