How to use the precompiled instruction in Delphi

zhaozj2021-02-11  205

In Delphi, there are similar pre-compiled instructions with C, although this type of instruction is only valid in the current single file (it is also possible that the author does not fully understand the true usage of the class instruction), this type of instruction is multi-version Production work (such as from the standard version of the Learning version), it does have a quite good use.

One. Instructions:

1. DEFINE Directive:

Format: {$ define name}

Description: Used to define a symbol (Symbol) that is valid in the current unit. Defined

The IF DEF and IFNDEF instructions can then be used to determine if the symbol exists.

2. Undef instruction:

Format: {$ Undef Name}

Description: Used to cancel a symbol (Symbol) that has been defined in the current unit. This directive and define

With the use of.

3. IFDEF instruction:

Format: {$ IFDEF Name}

Description: If the name after the instruction has been defined, compile the code to the code segment between {$ else} or {$ ENDIF}.

4. IFNDEF instruction:

Format: {$ IFNDEF Name}

Note: If the name after the instruction is not defined, the code segment between {$ else} or {$ Endif} is compiled.

5. IFopt Directive:

Format: {$ IFOPT switch}

Note: If the switch after the instruction has been set, the code segment is compiled until {$ ELSE} or {$ Endif}.

Example: {$ IFOPT R }

Writeln ('Check the Switch "at the time of compile");

{$ ENDIF}

6. ELSE directive:

Format: {$ else}

Note: Whether the code segment between the instruction to {$ Endif} should be compiled or ignored by determining the prefix IFXXX.

7. ENDIF instruction:

Format: {$ ENDIF}

Description: Team with IFXXX, indicating the end position of the conditional precompiled segment source code segment.

two. example:

Preparation examples, perform compilation of code segments by pre-defining different compilation symbols.

1. Create a Delphi project, add a Button button on the form of the Unit1 unit.

2. The writer is 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 character part is the code entered.

3. After compiling, press Button, see the form header bar display "AAA". The program compiled A: = 'AAA' statement.

4. Change the block of definition line:

Be changed to

{$ Define bbb}

When you compile it again, you see the "BBB" in the form header bar. The program compiled A: = 'bbb' statement.

When the definition line or change

{$ Define nothing}

When or other name, compile the run again, then see the form header bar display "Other". The program is only compiled with A: = 'Other' statements.

three. How to quickly produce and change versions:

When using the pre-compiled instruction, when making the same program, you only need to find out the units in each version, define a unified version symbol (Symbol), and then add the conditional precompiled instruction in the block. You can have a good role in the actual compilation of different program parts, so that the program's normative (definition of unified version symbol) and confidentiality (different version of the program part) have a good role.

However, since the precompiled instruction can only act on the current unit, it is inconvenient in that the version symbol cannot be defined in one public unit, but must define a unified version symbol in each unit, so this, when the version is replaced, It is necessary to determine that all version symbols have changed so that the correctness of each version is guaranteed. In this regard, you can use the functionality of Delphi IDE "Find In Files ..." to find all defined versions. The file and location of the symbol, then change it to ensure that all locations have been corrected.

转载请注明原文地址:https://www.9cbs.com/read-3747.html

New Post(0)