Go Delphi Pre-compilation Instruction summary

Source: Internet
Author: User

Delphi Pre-compilation Instruction summary

Delphi as a good development tool, not only because it pioneered the "component (Component)" era, the impact is far-reaching, another reason is that it has a very good compiler, Borland (Inprise) company claims that Delphi has the world's fastest compiler, If you have used Visual C + + and Delphi You will obviously appreciate this. Delphi's fast and efficient compiler mainly comes from the rigor of Object Pascal, using Delphi to communicate with the compiler at any time, and in most cases we do not need to interfere with the compiler's operation, but sometimes we need to make the necessary settings for the compiler. For ordinary programs, Delphi does not require programmers to add compiler instructions, Delphi has been automatically completed, but to get high-quality applications or have special requirements of the program must be familiar with the Delphi compiler instructions. The benefits of using compilation options are:

1. Assist in debugging: often in the process of developing the application system, especially to add some debugging procedures or feedback checking procedures, these debugging procedures for software quality improvement has a very positive function. However, if these additional programs are not needed in the official version of the development process, it is not only tiring, but also error-prone, to find out which programs are being used in a stack of programs, and to be deleted or set as annotations. At this point, if you can apply a Compiler such as $ifdef directives, can easily instruct Delphi to/do not put a piece of program into the execution file. The,compiler itself also provides a number of error-checking switches that can be used to alert programmers to possible problems in the program, as well as help in writing the correct program.

2. Version classification: For the version classification such as "trial Version", "Popular edition" and "Professional Edition", it can also be used by compiler directive to set different usage rights for the final product. Other global version management, such as "Chinese version", "Japanese version" and "International Standard Edition", can also instruct Delphi to specifically link which resource files or which appropriate programs to use.

{$IFDEF WIN32}

{$R XXX32. DCR}

{$ELSE}

{$R XXXX16. DCR}

{$EDNIF}

3. The reuse and management of the program: through compiler like {$I xxxx} directives, We can also include some programs, the same help to cut the combination of me ?? the program or compilation settings.

4. Set up a unified execution environment: the unit or program may be compiled on different machines, directly add the compiler directives to the program, not only to eliminate the need to specifically change the IDE before the application of the trouble, more importantly, to resolve the requirements of different compilation environment between the differences.

Using the Compile Settings dialog box

The compiler's compiler directives are used to specify the compiler's actions and behavior for the project compilation process. Can be set through the [Project]->[options]->[complier] Options page, most of the compilation environment can be adjusted through this dialog box, he contains the code, syntax, debugging information, and other settings.

1. Code Generation

Optimization: code optimization switch

Aligned record fields: Align the data. This compilation instruction is able to toggle between the byte alignment and the word alignment of variables and typed constants, which is global in scope.

Stack frames:windows stacks frame. Its scope is local, and he makes the compiler a far-end procedure and function to generate a specific opening and closing code. This instruction is used for Windows 3.0 real mode, and for all Delphi applications he should be closed.

Pentium-safe fdiv:pentium safety FDIV check. This directive is able to specify in the Pentium processor whether the compiler creates code that detects and uses a defective floating-point division instruction.

2. Run-time error (runtime Errors)

Range Checking: Range check. The scope of this instruction is local, and is used to control the generation of the range test code.

I/O checking:i/o check. The scope of this instruction is local, which is used to generate code that checks the input and output of a file and the result of the call. It should generally be turned on.

Overflos Checking: Overflow check. The function is to generate code for the arithmetic overflow check.

3. Syntax settings (Syntax optings)

Strict var-strings: Static variable string switch. Used to control the type checking of strings passed as variable arguments.

Comlete Boolean Eval: Full Boolean expression determination. A complete expression determination used to perform coercion. A complete Boolean determination, which can cause a system crash, is generally not used by him.

Extended Syntax: Extended syntax switch. He can allow or prohibit Delphi's extended syntax.

Typed @ Operator: typed @ operator check. Applies to the local variable reference, which controls the type of pointer value returned by the @ operator.

Open Parameters: Opening parameter switch. Used to control variable arguments declared using the string keyword. Open parameters allow you to pass variable-sized string variables to the same procedure or function, which is typically used in the assembly.

Huge Strings: String type switch. Used to control the switching of ansistring and shortstring types. Complies with Ansistring when turned on, shortstring when closed.

Assignable Typed Constants: An assignable type constant. For down-to-the-Delphi 1.0 compatibility.

4. Commissioning (Debugging)

Debug Information: Debugging information switch. Used to set whether debug information is written to a compiled unit file (. dcu).

Local Symbols: partial symbol switch. Allows or disables the creation of local symbolic information.

Reference info/definitions only: symbol information switch. Used to allow or disallow the generation of symbolic reference information used by the Delohi Object Browser.

Assertions: A property used to control local code.

Use Debug Dcus: Uses or disables DCU file debugging for VCL.

5. Information (Messages)

Show Hints: Show hint.

Show Warnings: Displays a warning.

Using the Compile directive (Compiler directives)

In addition to setting up the compiler using the Compile Settings dialog box, you can also set the compiler by compiling instructions. For local compiler settings, only compile instructions are used to complete. For switch compilation directives, the compiler is controlled by adding a plus and minus sign indicating the state of the switch after compiling the instruction. For example: {$B +}: Turns on full boolean checking. {$Q-}: Turn off overflow checking. In general, the scope of the compilation instruction is the code part after compiling the instruction, and for the whole process the compilation instruction should be against the beginning of the Unit interface section. The Compilation Settings dialog box has the corresponding compilation directives for setting up the compiler in the code, as shown below.

Set Item compilation directives

Optimizations {$O}

Aligned record fields {$A}

Stack Frames {$W}

Pentium-safe FDIV {$U}

Range Checking {$R}

I/o Checking {$I}

Overflow Checking {$Q}

Strict var-strings {$V}

Comlete Boolean Eval {$B}

Extended Syntax {$X}

Typed @ Operator {$T}

Open Parameters {$P}

Huge Strings {$H}

Assertions typed constants {$J}

Debug Information {$D}

Local Sysnbols {$L}

There are also some very useful compiler directives outside of these compilation directives.

$R Filename: This compilation directive is the most commonly used compilation directive, which is a resource file compilation directive that specifies the resource files that are connected to the execution files and libraries, such as {$R * in the project file (. DPR). RES}, indicating that the suffix is. Res A resource file with the same name as the project file is attached to the execution file, or a resource file can be specified, and the use of the resource file is a very important basis for writing Windows programs.

$I Filename: This compilation directive functions like the C language # include, which specifies the files to include at compile time.

The annotation structure of Delphi: {The content enclosed in this curly brace is a comment, which can be multiple lines, as long as it is possible between the two curly braces, the commented out content will generally be displayed in a special font and color (for all annotation types), easy to distinguish};(* The content between this parenthesis and the asterisk is the content of the comment *);//The content after this double backslash is the comment content, here only one row. Annotations cannot be nested. such as {{}} will not work, but (*{}*) is allowed.

There is also a comment that looks like a regular comment, but it has a special point starting with "{$" with "}", which is called a compilation instruction. If {$WARNINGS OFF}, it tells the compiler not to generate an error warning message. A compilation instruction is not a comment, but an indicator that tells the compiler how to compile it.

A compilation instruction is actually a marker (a marker that the compiler can recognize) and has its fixed syntax. The compiler compiles the program according to different instructions in different ways. You can place a compile instruction anywhere in the program (some should not be written in the unit, for a stack size such as {$MINSTACKSIZE} {$MAXSTACKSIZE}, or as {$APPTYE} to instruct the program to compile into graphics/text mode compiler Directives, can only be written in. In DPR, it is not effective to write in the unit. )。 The start character "$" of the compilation instruction, which always appears after the "{" symbol, is followed by the actual part of the compilation instruction, which can be one or more characters (depending on the instruction), and can contain one or more required parameters after the instruction. The Terminator of a compiled instruction is "}".

Delphi compiled instructions A total of three, switch compilation instructions, parameter compilation instructions and conditional compilation instructions, the following three kinds of compilation instructions are described

The Switch compiler command is characterized in that it has on, off two kinds of conversion state. For single-character versions, you can add "+" or "-" after the command is compiled. For long character versions, you can add "on" or "off" after compiling the instructions. The switch compiling instruction is divided into global and local two kinds. The global compilation Directive affects all compilation content, and it must precede the program and unit declarations. Local compilation directives only affect local compilation content, which is scoped to the next most recent compilation instruction. It can be placed anywhere in the program. The switch compilation directives can be combined into a simple set of compiled instructions, separated by commas, but not with spaces. For example: {$B +,r-,s-}

Parameter compilation directives specify what is affected by the parameters, such as file name or memory size. Conditional compilation directives Specify how the condition compiles a specific area segment. That is, if a condition is met, it is compiled in one way, otherwise it is compiled in another way. All compilation instructions (except the switch compiler Directive) should have at least one space between the name and the parameter. For example:

{$B +}

{$DEFINE Debug}

{$IFDEF Debug}

{$ENDIF}

Conditional compilation nesting can be up to 16 layers: in the use of {$IFDEF} ... {$ENDIF} When the condition compiles our program, one {$IFDEF} can contain another {$IFDEF}, but the depth can only be 16 layers, although it is a limitation, in normal circumstances, this should be enough.

Conditional compilation directives are very important for compiling instructions, and he controls the generation of different code under different conditions (for example, different operating systems). Conditional compilation directives are included in the

Comment in parentheses, as shown in the following table.

Conditional compilation directive meaning

$DEFINE is used to define a conditional symbol, and once defined, the condition symbol is true

$ELSE used in conjunction with $IFDEF, if the $ifdef condition is false, only a small fraction of the source file $else is compiled

$ENDIF end a conditional segment that starts with $if

$IFDEF to judge the condition symbol and compile the source file for True

$IFNDEF to judge the conditional symbol and compile the source file for the dummy

$IFOPT compile the source file according to the state of the compilation switch

$UNDEF undo a previous conditional symbol definition

These conditional compilation directives are very useful. For example, you can control the compilation by the state of the switch:

{ifopt r+}

ShowMessage (' Compiled with range-checking ');

{$ENDIF}

You can also control compilation by defining conditional symbols:

{$Define S}

......

{$ifdef S}

ShowMessage (' yes ');

{$else}

ShowMessage (' no ');

{$ENDIF}

The result of his compilation is ' Yes ', but if {$Define s} is omitted, ' No ' is displayed.

Some key conditional symbols have been pre-defined in Delphi, as shown in the following table.

Conditional symbol meaning

verxx compiler version, XX represents version, for example: Delphi 1.0 Compiler Version 80, Delphi 5.0 Compiler version 130

WIN32 whether the WIN32 run environment (Windows 95.98/nt/2000)

CPU386 whether the processor is Intel386 or above

Console program is consoles

Go Delphi Pre-compilation Instruction summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.